AWS Java SDK使用元数据进行上载时出错

时间:2014-04-23 08:51:17

标签: java amazon-web-services amazon-s3

我正在尝试将文件上传到S3容器,在上传之前,我正在设置文件的元数据。上传失败,并显示签名不匹配的错误。以下是我正在使用的代码:

public URL send(File f, HashMap<String,String> metadata, String type) throws Exception {
    String path = type+"/"+f.getName();

    InitiateMultipartUploadRequest req = new InitiateMultipartUploadRequest(container, secretKey).withKey(path);
    req.setCannedACL(CannedAccessControlList.AuthenticatedRead);

    if (metadata != null) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        Set<String> keys = metadata.keySet();
        Iterator<String> i = keys.iterator();
        while (i.hasNext()) {
            String key = i.next();
            objectMetadata.addUserMetadata(key, metadata.get(key));
        }
        req.setObjectMetadata(objectMetadata);
    }


    InitiateMultipartUploadResult res = s3client.initiateMultipartUpload(req);

    String uploadId = res.getUploadId();
    long fileSize = f.length();
    //check the size doesn't exceed max limit
    if (fileSize > MAX_OBJ_SIZE) {
        throw new Exception("Object size exceeds repository limit");
    }
    long chunkSize = 1024 * 1024 * 16;
    int chunks = (int) (fileSize/chunkSize + 2);
    List<PartETag> chunkList = new ArrayList<PartETag>();
    long pos = 0; 
    try {
        for (int i = 1; i < chunks; i++) {

            if ((chunks -i) < 2) {
                chunkSize = fileSize - pos;
            }

            UploadPartRequest upReq = new UploadPartRequest()
                    .withBucketName(container).withKey(path)
                    .withUploadId(uploadId).withPartNumber(i)
                    .withFileOffset(pos).withFile(f)
                    .withPartSize(chunkSize);

            PartETag pTag = null;
            // repeat the upload until it succeeds.
            boolean repeat;  
            do {
                repeat = false;  // reset switch
                try {
                    // Upload part and add response to our list.
                    pTag =   s3client.uploadPart(upReq).getPartETag(); 
                } 
                catch (Exception ex) {
                    repeat = true; // repeat
                }
            } while (repeat);

            chunkList.add(pTag);
            pos = pos + chunkSize;
        }
        CompleteMultipartUploadRequest compl = new CompleteMultipartUploadRequest(
                container, secretKey, uploadId, chunkList).withKey(path);
        CompleteMultipartUploadResult  complRes = s3client.completeMultipartUpload(compl);
        return new URL(URLDecoder.decode(complRes.getLocation(), "UTF-8"));
    }
    catch (Exception ex) {
        s3client.abortMultipartUpload(new AbortMultipartUploadRequest(container, 
                secretKey, uploadId));
        throw new Exception("File upload error: "+ex.toString());
    }
}

以下是我得到的错误:

 com.amazonaws.services.s3.model.AmazonS3Exception: Status Code: 403, AWS Service: Amazon S3, AWS Request ID: 0805716BBD0662AB, AWS Error Code: SignatureDoesNotMatch, AWS Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method., S3 Extended Request ID: wNAzUyrLZgWCazZFe3KpMHO0uh0FM5FF7fiwBzN1A2YDEYS5hKZBYh5nWSjIhnhG
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:767)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:414)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3316)
at com.amazonaws.services.s3.AmazonS3Client.initiateMultipartUpload(AmazonS3Client.java:2401)
at net.timbusproject.storage.awss3.S3Client.send(S3Client.java:134)

S3Client.java中发生错误的第134行是:

 InitiateMultipartUploadResult res = s3client.initiateMultipartUpload(req);

如果我没有附加任何元数据,上传工作正常。即,如果我评论下面的行,上传工作:

 req.setObjectMetadata(objectMetadata);

我无法确定设置元数据时请求失败的原因。我错过了上传过程中的任何步骤吗?

1 个答案:

答案 0 :(得分:5)

我能够通过URL编码元数据键和值来解决此问题。

objectMetadata.addUserMetadata(URLEncoder.encode(key, "UTF-8"), URLEncoder.encode(metadata.get(key),"UTF-8"));

显然,元数据似乎有一些违反AWS调用的违规字符。此解决方法将允许上载完成而不会出现错误,并且还会更新元数据,但字符串仍然是url编码的,这可能会在以后出现问题。