简体   繁体   English

使用 aws-sdk 在 s3 中托管 static 网站

[英]static website hosting in s3 using aws-sdk

I am trying to perform static website hosting on s3 that has a simple image file, using aws-sdk from my local ide.我正在尝试使用本地 ide 中的 aws-sdk 在具有简单图像文件的 s3 上执行 static 网站托管。 The code runs without any error and it reflects on my console as well but when i open the endpoint from console it doesn't open the file.代码运行没有任何错误,它也反映在我的控制台上,但是当我从控制台打开端点时,它不会打开文件。 The file is in the bucket as well and i have enabled public access.该文件也在存储桶中,我已启用公共访问。 But if i am doing the same through console i mean enabling static website hosting it opens.但如果我通过控制台做同样的事情,我的意思是启用 static 网站托管它打开。 This is the code...这是代码...

const aws = require('aws-sdk');
const s3 = new aws.S3();
const fs = require('fs');

const filecon = fs.readFileSync('C:\\Users\\Envy\\Desktop\\murugan.jpeg');

(async function(){

try {
    aws.config.setPromisesDependency();
    aws.config.update({
        region: 'us-east-1'
    })
    const a1 = await s3.createBucket({Bucket:'ommuruga'}).promise();
    const a2 = await s3.upload({Bucket:'ommuruga',Key:'murugan.jpeg',Body:filecon,ACL:'public-read'}).promise();
    const a3 = await s3.putBucketWebsite({Bucket:'ommuruga',WebsiteConfiguration:{IndexDocument:{Suffix:'murugan.jpeg'},ErrorDocument:{Key:'Error.html'}}}).promise();
    console.log(a3);
}       
catch(err){
    console.log(`unable to process err: ${err}`);
}
})();

You have to setup permissions for the s3 website to work as explained in AWS docs :您必须为 s3 网站设置权限才能按照AWS 文档中的说明工作:

  • disable S3 Block Public Access禁用 S3 阻止公共访问
  • add readonly bucket policy添加只读存储桶策略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM