Created
January 5, 2023 16:54
-
-
Save amirrajan/e1618abbf2aec540fdc29b29d1ba02f3 to your computer and use it in GitHub Desktop.
s3 Multipart Upload Testing
This file contains 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
// nodejs initialize aws client with api keys, then upload file to s3 | |
const fs = require('fs'); | |
const AWS = require('aws-sdk'); | |
const s3 = new AWS.S3({ | |
accessKeyId: '', | |
secretAccessKey: '' | |
}); | |
const uploadFile = async (fileName) => { | |
// Read content from the file | |
let uploadParts = { Parts: [] }; | |
// Setting up S3 upload parameters | |
const multiPartParam = { | |
Bucket: 'amirrajan', | |
Key: 'multipart.csv' | |
}; | |
let result = await s3.createMultipartUpload(multiPartParam).promise(); | |
const fileContent = fs.readFileSync('001.csv'); | |
const fileContent2 = fs.readFileSync('002.csv'); | |
const combinedBuffer = Buffer.concat([fileContent, Buffer.from("\n"), fileContent2]); | |
const params = { | |
Bucket: 'amirrajan', | |
Key: 'multipart.csv', // File name you want to save as in S3 | |
Body: fileContent, | |
PartNumber: 1, | |
UploadId: result.UploadId | |
}; | |
let uploadResult = await s3.uploadPart(params).promise(); | |
uploadParts.Parts.push({ ETag: uploadResult.ETag, PartNumber: 1 }); | |
// const fileContent2 = fs.readFileSync('002.csv'); | |
const params2 = { | |
Bucket: 'amirrajan', | |
Key: 'multipart.csv', // File name you want to save as in S3 | |
Body: fileContent2, | |
PartNumber: 2, | |
UploadId: result.UploadId | |
}; | |
console.log("uploading: ", params); | |
let uploadResult2 = await s3.uploadPart(params2).promise(); | |
uploadParts.Parts.push({ ETag: uploadResult2.ETag, PartNumber: 2 }); | |
const completeParam = { | |
Bucket: 'amirrajan', | |
Key: 'multipart.csv', | |
MultipartUpload: uploadParts, | |
UploadId: result.UploadId | |
}; | |
console.log("* STARTED - s3CompleteMultipartUploadAsync"); | |
console.log(JSON.stringify(completeParam, null, 2)); | |
await s3.completeMultipartUpload(completeParam).promise(); | |
console.log("* COMPLETE - s3CompleteMultipartUploadAsync"); | |
}; | |
uploadFile("main.js"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment