Skip to content

Instantly share code, notes, and snippets.

@ml4den
Created May 1, 2022 10:56
Show Gist options
  • Select an option

  • Save ml4den/558e197f4031d05d65c73fc293a9bf33 to your computer and use it in GitHub Desktop.

Select an option

Save ml4den/558e197f4031d05d65c73fc293a9bf33 to your computer and use it in GitHub Desktop.
Using aws-sdk to upload to a Scaleway S3 bucket in Node.js
const fs = require('fs');
import path from 'path';
const AWS = require('aws-sdk');
const BUCKET_NAME = 'my-bucket';
const awsConfig = new AWS.Config({
accessKeyId: process.env['SCALEWAY_ACCESS_KEY'],
secretAccessKey: process.env['SCALEWAY_SECRET_KEY'],
region: 'fr-par', // Not actually required to work
//s3BucketEndpoint: true, // Required if configuring for a single bucket endpoint like https://my-bucket.s3.fr-par.scw.cloud
endpoint: 'https://s3.fr-par.scw.cloud'
});
const s3 = new AWS.S3(awsConfig);
const uploadFile = (fileName) => {
// Read content from the file
const fileContent = fs.readFileSync(fileName);
// Setting up S3 upload parameters
const params = {
Bucket: BUCKET_NAME,
Key: 'file.jpg', // File name you want to save on bucket
Body: fileContent
};
// Uploading files to the bucket
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
console.log(`File uploaded successfully. ${data.Location}`);
});
};
uploadFile(path.resolve('.', 'example/img.jpeg'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment