<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>AWS amazone S3 Bucket Upload,Save, Get, Delete file</h2>
</div>
</div>
import AWS from 'aws-sdk';
const S3 = new AWS.S3({
accessKeyId: 'AKIAUDRAUVRKFL2GITGT',
secretAccessKey: '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'
});
export const UploadedFileToAWS = (filename: string, data: Buffer | string,ContentType:'image/gif'|'image/png'|'application/json') => {
return new Promise((resolve, reject) => {
S3.upload({
Bucket: 'dev-nftbuilder',
Key: filename,
Body: data,
ContentType,
ACL: "public-read-write"
}, (err, dist) => {
if (err) {
reject(err);
} else {
resolve(dist.Location)
}
})
});
}
export const GetJsonDataFromAWS = (filename: string) => {
return new Promise((resolve, reject) => {
S3.getObject({
Bucket: 'dev-nftbuilder',
Key: filename
}, (err,data) => {
if (err) {
reject(err);
} else {
// @ts-ignore
let json = JSON.parse(data.Body.toString('utf-8'));
resolve(json);
}
})
});
}
export const DeleteFileFromAWS = (filename: string) => {
return new Promise((resolve, reject) => {
S3.deleteObject({
Bucket: 'dev-nftbuilder',
Key: filename
}, (err,data) => {
if (err) {
reject(err);
} else {
resolve(data)
}
})
});
}
Delete File : // let deletedJson = await DeleteFileFromAWS(`${exists._id}/json/${edition}.json`);
Get File : let metaData = await GetJsonDataFromAWS(`${collection._id}/json/_metadata.json`);
Save File : let url = await UploadedFileToAWS(`${exists._id}/json/_metadata.json`, JSON.stringify(newMetaData), 'application/json');