我面临的问题是,我有一项任务是将图像,视频,音频和PDF文件上传到S3存储桶。 此代码驻留在我的活动中:
S3Credentials s3Credentials = new S3Credentials(ACCESS_KEY,
SECRET_KEY, "");
S3BucketData s3BucketData = new S3BucketData.Builder()
.setCredentials(s3Credentials)
.setBucket(bucket)
.setKey(uploadToS3.getName())
.setRegion("us-west-2")
.build();
S3UploadService.upload(MainActivity.this, s3BucketData, uploadToS3, false);
以下代码用于将数据上传到S3存储桶:
if (intent == null) return;
final String action = intent.getAction();
if (!ACTION_UPLOAD.equals(action)) return;
S3BucketData s3BucketData = (S3BucketData) intent.getSerializableExtra(EXTRA_S3_BUCKET_DATA);
File file = (File) intent.getSerializableExtra(EXTRA_FILE);
boolean deleteFileAfter = intent.getBooleanExtra(EXTRA_DELETE_FILE, true);
S3Callback s3Callback = (S3Callback) intent.getSerializableExtra(EXTRA_S3_CALLBACK);
handleUpload(s3BucketData, file, deleteFileAfter, s3Callback);
private void handleUpload(S3BucketData s3BucketData, File file, boolean deleteFileAfter, S3Callback s3Callback) {
TransferManager transferManager = setUpAmazonClient(s3BucketData);
PutObjectRequest request = buildPor(s3BucketData, file, deleteFileAfter);
AccessControlList acl = new AccessControlList();
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
acl.grantPermission(GroupGrantee.AllUsers, Permission.Write);
request.withAccessControlList(acl);
Upload upload = transferManager.upload(request);
try {
upload.waitForCompletion();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (AmazonS3Exception s3e) {
// Possible Bad Digest. Retry
if (VERBOSE) {
Log.w(TAG, "AmazonS3Exception. retrying.");
Log.d(TAG, s3e.toString());
}
}
if (s3Callback == null) return;
Intent intent = new Intent(s3Callback.getActionCallback());
intent.putExtra(EXTRA_SERIALIZABLE, s3Callback.getExtra());
sendBroadcast(intent);
}
private TransferManager setUpAmazonClient(S3BucketData s3BucketData) {
System.setProperty(getString(R.string.s3_system_property), "true");
S3Credentials s3Credentials = s3BucketData.getS3Credentials();
BasicAWSCredentials basicSessionCredentials =
new BasicAWSCredentials(s3Credentials.getAccessKey(),
s3Credentials.getSecretKey());
ClientConfiguration s3Config = new ClientConfiguration();
s3Config.setSocketTimeout(600000);
AmazonS3Client s3Client = new AmazonS3Client(basicSessionCredentials, s3Config);
TransferManager transferManager = new TransferManager(s3Client);
Region region = Region.getRegion(Regions.fromName(s3BucketData.getRegion()));
transferManager.getAmazonS3Client().setRegion(region);
return transferManager;
}
private PutObjectRequest buildPor(S3BucketData s3BucketData, final File file, final boolean deleteFileAfter) {
final String bucket = s3BucketData.getBucket();
final String key = s3BucketData.getKey();
final PutObjectRequest por = new PutObjectRequest(bucket, key, file);
por.setGeneralProgressListener(new ProgressListener() {
final String url = String.format(getString(R.string.s3_format_url), bucket, key);
private long uploadStartTime;
@Override
public void progressChanged(com.amazonaws.event.ProgressEvent progressEvent) {
try {
if (progressEvent.getEventCode() == ProgressEvent.STARTED_EVENT_CODE) {
uploadStartTime = System.currentTimeMillis();
} else if (progressEvent.getEventCode() == com.amazonaws.event.ProgressEvent.COMPLETED_EVENT_CODE) {
long uploadDurationMillis = System.currentTimeMillis() - uploadStartTime;
int bytesPerSecond = (int) (file.length() / (uploadDurationMillis / 1000.0));
if (VERBOSE) {
double fileSize = file.length() / 1000.0;
double uploadDuration = uploadDurationMillis;
double uploadSpeed = bytesPerSecond / 1000.0;
Log.i(TAG, String.format(S3UploadService.this.getString(R.string.s3_format_uploaded), fileSize, uploadDuration, uploadSpeed));
}
if (deleteFileAfter) {
file.delete();
}
} else if (progressEvent.getEventCode() == ProgressEvent.FAILED_EVENT_CODE) {
if (VERBOSE) Log.e(TAG, String.format(S3UploadService.this.getString(R.string.s3_format_upload_failed), url));
}
} catch (Exception excp) {
if (VERBOSE) Log.e(TAG, "ProgressListener error");
excp.printStackTrace();
}
}
});
por.setCannedAcl(CannedAccessControlList.PublicRead);
return por;
}
public static void setVerbose(boolean verbose) {
S3UploadService.VERBOSE = verbose;
}
我可以使用上面的代码将数据上传到我的两个S3存储桶中。
但是,有一个存储桶在尝试上传数据时遇到错误:
“您尝试访问的存储区必须使用指定的端点进行寻址。请将所有将来的请求发送到此端点。(服务:Amazon S3;状态代码:301;错误代码:PermanentRedirect;请求ID: 69D73B8CE65BE315),S3扩展请求ID:54FOeNuCsR / QZxV19vcAi9RCEyNkbxuIGt5xDi8QjsZw17qwsenRF1mqZUSX3LJF1ThydZJVxIM = “