Skip to content

Instantly share code, notes, and snippets.

@Bucephalus-lgtm
Created January 12, 2023 09:06
Show Gist options
  • Save Bucephalus-lgtm/4938db7c9ec006f419d877c8df772b0c to your computer and use it in GitHub Desktop.
Save Bucephalus-lgtm/4938db7c9ec006f419d877c8df772b0c to your computer and use it in GitHub Desktop.
// To use the AWS SDK, first import it from aws-sdk npm package
import AWS from 'aws-sdk';
const s3Bucket = `__your_aws_s3_bucket_name__`;
const region = `__your_aws_region_name__`;
const urlExpirationTime = `__your_aws_s3_url_expiration_time__`
AWS.config.update({
accessKeyId: `__your_aws_access_key_id__`,
secretAccessKey: `__your_aws_secret_access_key__`,
})
const generatePreSignedPutUrl = (s3FileName: any, fileType: any) => {
try {
const signedUrl = myBucket.getSignedUrl('putObject', {
Key: s3FileName,
ContentType: fileType,
Expires: urlExpirationTime
});
return signedUrl;
} catch (error) {
console.log(error);
return "https://png.pngtree.com/png-vector/20220611/ourmid/pngtree-broken-link-icon-set-png-image_4823405.png";
}
}
const UploadImageToS3 = async (s3FileName: any, file: any, quality) => {
const presignedUrl = generatePreSignedPutUrl(fileName, 'image/jpg');
try {
const response = await fetch(presignedUrl, {
method: 'PUT',
body: file
});
return { status: 200, presignedUrlResponse: response };
} catch (err) {
console.error(err);
return { status: 500, err };
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment