Skip to content

Instantly share code, notes, and snippets.

@akosiyawin
Created February 19, 2023 00:48
Show Gist options
  • Save akosiyawin/96e16481b4ea46bfd8cc47556e12c3d6 to your computer and use it in GitHub Desktop.
Save akosiyawin/96e16481b4ea46bfd8cc47556e12c3d6 to your computer and use it in GitHub Desktop.
Upload file to AWS S3 with public permission.
import { S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
const parallelUploads3 = new Upload({
client: new S3Client({
region: "ap-southeast-2",
credentials: {
accessKeyId: props.accessKeyID,
secretAccessKey: props.secretKey,
},
}),
params: {
Bucket: props.bucketName,
Key: 'path/to/s3/file_name.jpg',
Body: form.file,
ACL: "public-read",
ContentType: form.file.type,
},
partSize: 1024 * 1024 * 5,
queueSize: 3,
partSize: 1024 * 1024 * 5, // optional size of each part, in bytes, at least 5MB
leavePartsOnError: false, // optional manually handle dropped parts
});
//track the progress
parallelUploads3.on("httpUploadProgress", (progress) => {
console.log(progress);
});
const data = await parallelUploads3.done();
data.Location //get public path/location
{
"dependencies": {
"@aws-sdk/client-s3": "^3.223.0",
"@aws-sdk/lib-storage": "^3.223.0",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment