我正在使用第三方服務器,它會返回以下內容。 1)網址 2)ACL 3)政策 4)awsAccesskeyID 5)簽名 6)鍵 我可以使用下面的代碼使用AmazonS3Client在S3上上傳文件java
final File localFile = new File(localFilePath);
final Part[] parts = { new StringPart("acl", acl),
new StringPart("policy", policy),
new StringPart("AWSAccessKeyId", awsAccessKeyId),
new StringPart("signature", signature),
new StringPart("key", key, HTTP.UTF_8),
new FilePart("file", localFile) };
final MultipartRequestEntity mpRequestEntity = new MultipartRequestEntity(parts, filePost.getParams());
filePost.setRequestEntity(mpRequestEntity);
final HttpClient client = new HttpClient();
try
{
status = client.executeMethod(filePost);
}
上傳文件,但現在我想用AmazonS3Client使用下面的代碼,但其拋出異常
10-31 16:21:36.070:INFO/com.amazonaws.request(13882):接收到錯誤響應 :狀態代碼:403,AWS請求ID:51F7CB27E58F88FD,AWS 錯誤代碼:SignatureDoesNotMatch,AWS錯誤消息:請求 我們計算的簽名與您提供的簽名不匹配。 檢查你的密鑰和簽名方法,S3擴展請求ID: YwNNsWOXg71vXY1VS0apHnHpHp4YVWRJ63xm8C7w36SYg1MNuIykw75YhQco5Lk7
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(awsAccessKeyId, key));
// Create a list of UploadPartResponse objects. You get one of these
// for each part upload.
final List<PartETag> partETags = new ArrayList<PartETag>();
// Step 1: Initialize.
final InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(targetURL, key);
final InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
final File file = new File(localFilePath);
final long contentLength = file.length();
long partSize = 5242880; // Set part size to 5 MB.
try
{
// Step 2: Upload parts.
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++)
{
// Last part can be less than 5 MB. Adjust part size.
partSize = Math.min(partSize, (contentLength - filePosition));
// Create request to upload a part.
final UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(targetURL).withKey(key)
.withUploadId(initResponse.getUploadId()).withPartNumber(i).withFileOffset(filePosition)
.withFile(file).withPartSize(partSize);
// Upload part and add response to our list.
partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
filePosition += partSize;
}
// Step 3: complete.
final CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(targetURL, key,
initResponse.getUploadId(), partETags);
s3Client.completeMultipartUpload(compRequest);
}
catch (final Exception e)
{
s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(targetURL, key, initResponse.getUploadId()));
return false;
}
return true;
我失去了一些東西在這裏?
做。我有誤解關於簽名和密鑰.. http://dextercoder.blogspot.in/2012/02/multipart-upload-to-amazon-s3-in-three.html – knocker
嗨,使用此代碼,我該如何暫停上傳和稍後恢復? – AndroidDev