Resources regarding upload to storj using nodejs

Hi, I’m new to Storj and I’m looking for guidance on how to upload data or files from a Node.js backend. I’m currently working on a React Native app similar to Google Photos and I’m interested in using Storj for storage purposes. Could you please provide me with some resources or guidance on how to achieve this? Thank you!

Hi!

You should use a lambda/serverless function that uses the S3 libraries - you can check out more info at Developing with Amazon S3 using the AWS SDKs, and explorers - Amazon Simple Storage Service

-K

2 Likes

Hi Karl,
Actually, my project requirement is to use Decentralized storage. That’s why, I opt for Storj…

Storj is a great choice and is S3 compatible. Using the AWS sdk is an easy way to interface.

Here is an example:

const AWS = require('aws-sdk');

// Replace these with your Storj S3 credentials
const accessKeyId = 'your-access-key-id';
const secretAccessKey = 'your-secret-access-key';
const endpoint = 'https://gateway.storjshare.io';

// Create an S3 client
const s3 = new AWS.S3({
  accessKeyId: accessKeyId,
  secretAccessKey: secretAccessKey,
  endpoint: endpoint,
  s3ForcePathStyle: true, // Needed for using a custom endpoint
  signatureVersion: 'v4'
});

// Example function to list buckets
async function listBuckets() {
  try {
    const data = await s3.listBuckets().promise();
    console.log('Buckets:', data.Buckets);
  } catch (err) {
    console.error('Error listing buckets:', err);
  }
}

// Example function to upload a file
async function uploadFile(bucketName, key, body) {
  const params = {
    Bucket: bucketName,
    Key: key,
    Body: body
  };

  try {
    const data = await s3.upload(params).promise();
    console.log('File uploaded successfully:', data);
  } catch (err) {
    console.error('Error uploading file:', err);
  }
}

// Example function to download a file
async function downloadFile(bucketName, key) {
  const params = {
    Bucket: bucketName,
    Key: key
  };

  try {
    const data = await s3.getObject(params).promise();
    console.log('File downloaded successfully:', data.Body.toString());
  } catch (err) {
    console.error('Error downloading file:', err);
  }
}

// Example usage
(async () => {
  await listBuckets();
  await uploadFile('my-bucket', 'my-key', 'Hello, Storj!');
  await downloadFile('my-bucket', 'my-key');
})();

3 Likes

Take a look at Sample NodeJS and React code using presigned urls and AWS SDK V3

It’ll get you up to speed with some sample code and a walk-through video.

3 Likes

Hello @azeem,
Welcome to the forum!

You may also try to use a native Storj integration using the uplink-nodejs binding:

2 Likes

Thanks danw… That will help a lot :heart:

1 Like

Thanks for this help :blush:

1 Like