我正在尝试使用以下代码将图像上传到amazon(AWS SDK 2.3.0)s3
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"XXXXXXX",
// Identity Pool ID
Regions.EU_WEST_1 // Region
);
AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
TransferUtility transferUtility = new TransferUtility(s3Client, this.
getApplicationContext());
TransferObserver observer = transferUtility.upload("testbucketzzzz", "test.jpg",
file);
每次我尝试上传图片时都会出现以下错误
UploadTask:无法上传:37由于您提供的授权机制不受支持。请使用AWS4-HMAC-SHA256。 (服务:Amazon S3;状态代码:400;错误代码:InvalidRequest;请求ID:9787E53737D02BA7)
我认为这只是旧版AWS SDK的问题。经过无数小时的尝试修复它后,我发现了以下"技巧"但这只会引发另一个错误,根本不需要。
System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
答案 0 :(得分:1)
您可以使用以下snipet将图片上传到Amazon S3
(确保您在MainThread
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(
ACCESS_KEY,
SECRET_KEY
);
AmazonS3Client s3Client = new AmazonS3Client(basicAWSCredentials);
s3Client.setEndpoint(ENDPOINT);
s3Client.setRegion(Region.getRegion(Regions.EU_WEST_1));;
PutObjectRequest por = new PutObjectRequest(
"YOUR_BUCKET_NAME", "YOUR_KEY", file);
}
s3Client.putObject(por);
如果您想了解更多有关您的方法无效的信息,请参阅this link