Created
June 14, 2023 04:47
-
-
Save saswata-dutta/6a526c9a9de688731bef9c7677231153 to your computer and use it in GitHub Desktop.
access aws s3 using js sdk v3
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
import { | |
S3Client, | |
PutObjectCommand, | |
GetObjectAttributesCommand, | |
GetObjectCommand, | |
DeleteObjectCommand, | |
ListObjectsV2Command, | |
} from "@aws-sdk/client-s3"; | |
import { | |
getSignedUrl, | |
} from "@aws-sdk/s3-request-presigner"; | |
const s3 = new S3Client({ region: "us-east-1" }); | |
const BKT = "my-test-bkt"; | |
const KEY = "f/hello.txt"; | |
export const handler = async(event) => { | |
const put = new PutObjectCommand({ | |
Bucket: BKT, | |
Key: KEY, | |
Body: "Hello world", | |
}); | |
console.log(await s3.send(put)); | |
const getAttrs = new GetObjectAttributesCommand({ | |
Bucket: BKT, | |
Key: KEY, | |
ObjectAttributes: ["ETag", "ObjectSize"], | |
}); | |
console.log(await s3.send(getAttrs)); | |
const getCmd = new GetObjectCommand({ Bucket: BKT, Key: KEY1 }); | |
const getResp = await s3.send(getCmd); | |
const data = await getResp.Body.transformToString(); | |
console.log(data); | |
const signedUrl = await getSignedUrl(s3, getCmd, { expiresIn: 60 }); | |
console.log(signedUrl); | |
const delCmd = new DeleteObjectCommand({ | |
Bucket: BKT, | |
Key: KEY, | |
}); | |
console.log(await s3.send(delCmd)); | |
const listCmd = new ListObjectsV2Command({ | |
Bucket: BKT, | |
Prefix: "f/", | |
StartAfter: "f/", | |
MaxKeys: 3, | |
}); | |
console.log(await s3.send(listCmd)); | |
}; |
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
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Sid": "VisualEditor0", | |
"Effect": "Allow", | |
"Action": "s3:ListBucket", | |
"Resource": "arn:aws:s3:::*" | |
}, | |
{ | |
"Sid": "VisualEditor1", | |
"Effect": "Allow", | |
"Action": [ | |
"s3:PutObject", | |
"s3:GetObject", | |
"s3:GetObjectAttributes", | |
"s3:DeleteObject" | |
], | |
"Resource": "arn:aws:s3:::*/*" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment