ios - AWS S3 存储桶问题

标签 ios objective-c amazon-web-services amazon-s3 aws-sdk

谁能帮我找到如何使用客户端 Access key IDSecret access key 访问 AWS S3 Bucket 资源? 或者这是已弃用的 AWS API?我找到了很多解决方案,但都使用了我不想使用的 identityPoolId

最佳答案

您可以使用两种方式登录。

  1. 在您的代码中使用凭据。
  2. 将您的凭据保存在一个文件中。

通过在您的代码中使用凭据。

// credentials object identifying user for authentication
// user must have AWSConnector and AmazonS3FullAccess for 
// this example to work
 AWSCredentials credentials = new BasicAWSCredentials("YourAccessKeyID", "YourSecretAccessKey");

// create a client connection based on credentials
AmazonS3 s3client = new AmazonS3Client(credentials);

将您的凭据保存在一个文件中:

/*
 * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) 
 * and save the following lines after replacing the underlined values with your own.
 *
 * [default]
 * aws_access_key_id = YOUR_ACCESS_KEY_ID
 * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
 */

AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials();
AmazonS3 s3 = new AmazonS3Client(credentials);

crud操作可以引用这个教程:https://github.com/aws/aws-sdk-java/blob/master/src/samples/AmazonS3/S3Sample.java

    // Create a bucket
    System.out.println("Creating bucket " + bucketName + "\n");
    s3.createBucket(bucketName);

    /*
     * List the buckets in your account
     */
    System.out.println("Listing buckets");
    for (Bucket bucket : s3.listBuckets()) {
        System.out.println(" - " + bucket.getName());
    }
    /*
     * Delete an object - Unless versioning has been turned on for your bucket,
     * there is no way to undelete an object, so use caution when deleting objects.
     */
    System.out.println("Deleting an object\n");
    s3.deleteObject(bucketName, key);

    /*
     * Delete a bucket - A bucket must be completely empty before it can be
     * deleted, so remember to delete any objects from your buckets before
     * you try to delete them.
     */
    System.out.println("Deleting bucket " + bucketName + "\n");
    s3.deleteBucket(bucketName);

具体bucket的权限,请看图:

enter image description here

添加权限:

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::riz-bucket001/*"
        }
    ]
}

注意:此存储桶策略使存储桶中的所有内容都可公开读取。所以要小心使用它。如果你以学习为目的,那OK。但出于商业目的,请勿使用它。

非常感谢Michael - sqlbot

您可以根据需要在此处查看更多政策:Specifying Permissions in a Policy

关于ios - AWS S3 存储桶问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40803668/

相关文章:

ios - 尝试在 Swift 中访问嵌套字典数组中的键

iphone - OpenGL ES代码修改

iphone - 将图 block 坐标转换为像素再转换为地理坐标

ios - viewDidAppear 调用了在 iOS8 中被关闭的 View Controller

objective-c - Quicklook 嵌入式预览

amazon-web-services - 将来自远程资源的对象放入 Amazon S3

ios - NSDecimalNumber decimalNumberWithString : ignores current locale

ios - UIImagePickerController:如何允许编辑但隐藏裁剪选项

amazon-web-services - 将 SSL 证书应用于 AWS EC2 Ubuntu 实例

java - AWS EMR YARN 容量调度程序配置