Created
May 1, 2022 10:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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