【问题标题】:S3 - PutObject results in 403 - forbidden - public access?S3 - PutObject 导致 403 - 禁止 - 公共访问?
【发布时间】:2021-02-25 22:29:04
【问题描述】:

我对 S3 存储桶隐私设置感到困惑。

我创建了一个阻止所有公共访问的存储桶,并且我创建了一个具有AmazonS3FullAccess 权限的 IAM 用户。

然后我使用该 IAM 用户的 Access KeyAccess Secret 将对象放入存储桶中,但这会失败,因为 PutObject 返回 403 禁止错误。我不明白为什么,因为用户具有完全访问 S3 的角色。

我的存储桶权限如下所示:

所以现在我有两个问题:

  1. 我是不是忘记了一些东西才能让它工作?
  2. 我知道当我启用公共访问时它会起作用 - 但这究竟意味着什么?任何 S3 用户都可以访问我的存储桶以下载/上传/访问该存储桶中的文件,还是只能访问 ACL 中显示的用户 - 目前只是存储桶的所有者?

感谢您的澄清!

【问题讨论】:

  • "IAM 用户在存储桶中放置对象" - 您如何放置这些对象?使用 AWS 控制台、AWS CLI 还是 SDK?
  • 我认为我的电子商务系统使用了 SDK。

标签: amazon-web-services amazon-s3


【解决方案1】:

您没有指定如何将对象放入存储桶中。让我们在这里做一个假设。假设您想使用代码来执行此任务。现在假设您在 Windows 上的 C:\Users\USERNAME.aws\credentials 或 Linux、macOS 或 Unix 上的 ~/.aws/credentials 中名为 credentials 的文件中设置 IAM 凭据。假设您按照此文档设置 IAM 用户:

https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started_create-admin-group.html#getting-started_create-admin-group-console

(我怀疑您对 IAM 的设置有误 - 请查看此文档)

您可以使用此代码将对象上传到 Amazon S3 存储桶(这是 Java 示例 - 但您可以找到不同编程语言的其他示例)。

import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
    
public class PutObject {

    public static void main(String[] args) {
        final String USAGE = "\n" +
                "Usage:\n" +
                "  PutObject <bucketName> <objectKey> <objectPath> \n\n" +
                "Where:\n" +
                "  bucketName - the Amazon S3 bucket to upload an object into.\n" +
                "  objectKey - the object to upload (for example, book.pdf).\n" +
                "  objectPath - the path where the file is located (for example, C:/AWS/book2.pdf). \n\n" ;

        if (args.length != 3) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String bucketName = args[0];
        String objectKey = args[1];
        String objectPath = args[2];

        System.out.println("Putting object " + objectKey +" into bucket "+bucketName);
        System.out.println("  in bucket: " + bucketName);

        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder()
                .region(region)
                .build();

        String result = putS3Object(s3, bucketName, objectKey, objectPath);
        System.out.println("Tag information: "+result);
        s3.close();
    }

    // snippet-start:[s3.java2.s3_object_upload.main]
    public static String putS3Object(S3Client s3,
                                     String bucketName,
                                     String objectKey,
                                     String objectPath) {

        try {

            Map<String, String> metadata = new HashMap<>();
            metadata.put("myVal", "test");

            PutObjectRequest putOb = PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .metadata(metadata)
                    .build();

            PutObjectResponse response = s3.putObject(putOb,
                    RequestBody.fromBytes(getObjectFile(objectPath)));

           return response.eTag();

        } catch (S3Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }

    // Return a byte array
    private static byte[] getObjectFile(String filePath) {

        FileInputStream fileInputStream = null;
        byte[] bytesArray = null;

        try {
            File file = new File(filePath);
            bytesArray = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesArray);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bytesArray;
    }
}

【讨论】:

  • "您没有指定如何将对象放入存储桶中。" -> 你到底是什么意思?
猜你喜欢
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 2012-09-11
  • 2012-02-28
相关资源
最近更新 更多