2017-02-24 46 views
1

在我們的項目中,我們配置了Amazon webservice。在Android應用程序端,它需要生成s3 url以從服務器獲取圖像。通過使用下面的代碼,我試圖獲取圖像,但每次遇到Signature失敗或任何其他異常。生成S3 URL以在Android中獲取圖像

//Set the google token in map 
      Map<String, String> logins = new HashMap<>(); 
      logins.put(AppConstants.GOOGLE_CONSTANT, token); 

      //Initialize amazon cognito credentials provider 
      CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
        activity, // Context 
        AppConstants.IDENTITY_POOL_ID, // Identity Pool ID 
        Regions.US_EAST_1 // Region 
      ); 

      //Set the google credentials 
      credentialsProvider.setLogins(logins); 

AmazonS3 s3client = new AmazonS3Client(credentialsProvider); 

java.util.Date expiration = new java.util.Date(); 
long msec = expiration.getTime(); 
msec += 1000 * 60 * 60; // 1 hour. 
expiration.setTime(msec); 

GeneratePresignedUrlRequest generatePresignedUrlRequest = 
      new GeneratePresignedUrlRequest(bucketName, objectKey); 
generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default. 
generatePresignedUrlRequest.setExpiration(expiration); 

URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest); 

這裏,當我嘗試在瀏覽器中加載接收URL時,它會拋出一個Signature錯誤。

問題是,所有的幫助都是針對Java的。我沒有找到任何Android的具體幫助。

我剛纔提到以下站點 https://aws.amazon.com/blogs/developer/generating-amazon-s3-pre-signed-urls-with-sse-kms-part-2/ http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLJavaSDK.html

回答

0

您可以使用下面的代碼用於獲取預籤取得網址。

URL presignedUrl = s3.generatePresignedUrl(
      bucketName, key, new Date(new Date().getTime() + 1000 * 60 * 60)); 
System.out.println(presignedUrl.toExternalForm()); 

HttpURLConnection connection = connectToPresignedUrl(
      s3, new GeneratePresignedUrlRequest(bucketName, key)); 
assertEquals(200, connection.getResponseCode());