2016-04-25 42 views
0

我想建立連接到aws以在s3存儲桶上執行基本操作。以下是代碼:使用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. 

我也雙重檢查,我使用的訪問密鑰和祕密密鑰。無法找出問題。

+0

你有沒有檢查http://stackoverflow.com/questions/2777078/amazon-mws-request-signature-calculated-does-not-match-the-signature-provided –

回答

-1

它總是很好用「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/

不要忘記添加憑據信息:

帳戶ID:@「XXXXXXXX」 identityPoolId:@」 XXXXXXXX-XXXXXXXX」 unauthRoleArn:@」 XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX「

+0

IAM的用戶和角色是一樣靈活,和通常是在這種情況下使用的。 –

+0

@tauheed剛剛提供了另一種選擇。同意馬克! –