const AWS = require("aws-sdk");
const ID = "@@";
const SECRET = "@@";
const BUCKET_NAME = "@@";
const s3 = new AWS.S3({ accessKeyId: ID, secretAccessKey: SECRET });
const params = {
Bucket: BUCKET_NAME,
CreateBucketConfiguration: {
LocationConstraint: "ap-northeast-2",
},
};
s3.createBucket(params, function (err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log("Bucket Created Successfully", data.Location);
}
});
const fs = require("fs");
const AWS = require("aws-sdk");
const ID = "@@";
const SECRET = "@@";
const BUCKET_NAME = "testbucket00917";
const s3 = new AWS.S3({ accessKeyId: ID, secretAccessKey: SECRET });
const uploadFile = (fileName) => {
const fileContent = fs.readFileSync(fileName);
const params = {
Bucket: BUCKET_NAME,
Key: "test2.txt", // S3에 업로드 되었을 때 저장될 파일 이름
Body: fileContent,
};
s3.upload(params, function (err, data) {
if (err) {
throw err;
}
console.log(`File uploaded successfully. ${data.Location}`);
});
};
uploadFile("./test.txt");
const fs = require("fs");
const AWS = require("aws-sdk");
const ID = "@@";
const SECRET = "@@";
const BUCKET_NAME = "testbucket00917";
const s3 = new AWS.S3({ accessKeyId: ID, secretAccessKey: SECRET });
const downloadFile = (fileName) => {
const params = {
Bucket: BUCKET_NAME,
Key: "test2.txt", // 버켓에서 어떤 파일을 가져올 것인지
};
s3.getObject(params, function (err, data) {
if (err) {
throw err;
}
fs.writeFileSync(fileName, data.Body.toString());
});
};
downloadFile("./test3.txt"); // parameter로 어떤 이름으로 local에 저장할 것인지
https://stackabuse.com/uploading-files-to-aws-s3-with-node-js/
https://brunch.co.kr/@daniellim/43