2013-03-29 101 views
0

我是AWS的新手。現在我想編寫一個Java代碼來使用CloudFront訪問s3。如何讓CloudFront訪問某些S3?

我創建了S3存儲桶。但我不知道如何使用S3憑證獲取雲端對象。

我讀了AWS的Java API,它似乎是代碼應該是這種形式:

AmazonCloudFrontClient cloudfront = new AmazonCloudFrontClient(credentials); 
CreateCloudFrontOriginAccessIdentityRequest originRequest = new CreateCloudFrontOriginAccessIdentityRequest(); 
originRequest.setRequestCredentials(credentials); 

cloudfront.createCloudFrontOriginAccessIdentity(originRequest); 

但我沒有看到一個S3ID或東西的S3設置到CloudFront的。

回答

1

如果您想使用CloudFront爲您的S3文件提供服務,通常意味着您希望S3存儲桶公開可用。通過S3接口(Web控制檯,API或第三方工具,如CloudBerryBucket Explorer),您可以簡單地將對象和存儲桶定義爲公共。

您也可以在Java SDK

Statement allowPublicReadStatement = new Statement(Effect.Allow) 
     .withPrincipals(Principal.AllUsers) 
     .withActions(S3Actions.GetObject) 
     .withResources(new S3ObjectResource(myBucketName, "*")); 

Policy policy = new Policy() 
     .withStatements(allowPublicReadStatement); 

AmazonS3 s3 = new AmazonS3Client(myAwsCredentials); 
s3.setBucketPolicy(myBucketName, policy.toJson()); 

設置,如果你想爲私人文件,你可以檢查這裏的單證:http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html