You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Node.js使用AWS SDK修改S3存储桶公共访问设置?

解决S3存储桶「阻止所有公共访问」的配置问题

方案1:创建存储桶后立即解除公共访问限制

在createBucket的成功回调中调用putPublicAccessBlock接口,将所有公共访问阻止选项设为false,即可解除默认的公共访问限制:

var bucketParams = {
    Bucket: 'bucketname123'
};

s3.createBucket(bucketParams, function (err, data) {
    if (err) {
        console.log("Error creating bucket", err);
    } else {
        console.log("Success", data.Location);
        // 配置公共访问设置,解除所有阻止规则
        const publicAccessParams = {
            Bucket: 'bucketname123',
            PublicAccessBlockConfiguration: {
                BlockPublicAcls: false,
                IgnorePublicAcls: false,
                BlockPublicPolicy: false,
                RestrictPublicBuckets: false
            }
        };
        s3.putPublicAccessBlock(publicAccessParams, function (err) {
            if (err) {
                console.log("Error updating public access settings", err);
            } else {
                console.log("Public access restrictions removed successfully");
                updateWebConfig();
            }
        });
    }
});

方案2:单独修改已存在存储桶的设置

如果需要对已创建完成的存储桶调整公共访问规则,直接调用putPublicAccessBlock接口即可:

const publicAccessParams = {
    Bucket: 'bucketname123',
    PublicAccessBlockConfiguration: {
        BlockPublicAcls: false,
        IgnorePublicAcls: false,
        BlockPublicPolicy: false,
        RestrictPublicBuckets: false
    }
};

s3.putPublicAccessBlock(publicAccessParams, function (err) {
    if (err) {
        console.log("Error updating public access settings", err);
    } else {
        console.log("Public access restrictions removed successfully");
    }
});

关键注意事项

  • 确保你的IAM身份(用户/角色)拥有s3:PutPublicAccessBlock权限,否则会返回权限错误
  • 若只需解除部分公共访问限制,可单独调整PublicAccessBlockConfiguration中的对应参数(比如仅将BlockPublicAcls设为false)

内容的提问来源于stack exchange,提问作者Hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 04:52:05