s3-object-readonly 권한이 있는 역할을 지정console.log('Loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = async (event, context) => {
// console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
try {
const { ContentType } = await s3.getObject(params).promise();
console.log('ContentType:', ContentType);
return ContentType;
} catch (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
throw new Error(message);
}
};
params 라는 변수에 지정s3.getObject() 라는 함수를 통해 버킷 내용을 읽어옴ContentType 을 읽어왔지만, Body 값을 읽어올 수도 있음Body 값은 그러나, .gz 으로 압축되어있어 unzip 과정이 필요함.<aside> 💡 참고자료 Unzip lambda 블로그
</aside>