使用 Grails 的 AWS S3

AWS S3 using grails

我正在尝试建立与 aws 的连接以在 s3 存储桶上执行基本操作。以下是代码:

def list(){
        AWSCredentials credentials = new BasicAWSCredentials("Access key", "Secret Key");


        AmazonS3 s3client = new AmazonS3Client(credentials);


        String bucketName = "sample-bucket-from-java-code";



        System.out.println("Listing all buckets : ");
        for (Bucket bucket : s3client.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
    }

这给了我错误:

request- Received error response: com.amazonaws.services.s3.model.AmazonS3Exception: The request signature we calculated does not match the signature you provided. Check your key and signing method.

我还仔细检查了我正在使用的访问密钥和秘密密钥。无法找出问题。

使用 "cognito accountId" 总比使用访问密钥和密钥好。因为使用 cognito accountID 限制了对 AWS 的访问,它总是可以更改的。

// 初始化 Amazon Cognito 凭证提供程序。

AWSCognitoCredentialsProvider* credentialsProvider = [AWSCognitoCredentialsProvider
                                                      credentialsWithRegionType:AWSRegionUSEast1
                                                      accountId:@"xxxxxxxxxxx"
                                                      identityPoolId:@"xxxxxxxxxxx"
                                                      unauthRoleArn:@"arn:aws:iam::xxxxxxxxxxx"
                                                      authRoleArn:nil];

AWSServiceConfiguration* configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSWest2
                                                                      credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

您还可以从我的博客中找到 运行 代码。

https://tauheeda.wordpress.com/2015/10/15/use-aws-service-to-downloadupload-files-in-your-applications/

不要忘记添加您的凭据信息:

accountId:@“xxxxxxxx” identityPoolId:@”xxxxxxxx-xxxxxxxx” unauthRoleArn:@”xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx“