AWS Java SDK使用元数据上传时出错

Sri*_*ara 4 java amazon-s3 amazon-web-services

我正在尝试将文件上传到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());
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我得到的错误:

 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)
Run Code Online (Sandbox Code Playgroud)

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

 InitiateMultipartUploadResult res = s3client.initiateMultipartUpload(req);
Run Code Online (Sandbox Code Playgroud)

如果我不附加任何元数据,则上传工作正常。即,如果我评论以下内容,则上传有效:

 req.setObjectMetadata(objectMetadata);
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么在设置元数据时请求失败。我是否错过了上传过程中的任何步骤?

Sri*_*ara 5

我可以通过对元数据键和值进行URL编码来解决此问题。

objectMetadata.addUserMetadata(URLEncoder.encode(key, "UTF-8"), URLEncoder.encode(metadata.get(key),"UTF-8"));
Run Code Online (Sandbox Code Playgroud)

显然,元数据似乎包含一些令人讨厌的字符,这些字符使AWS调用变得混乱。此解决方法将使上传完成而不会出现错误,并且还会更新元数据,但字符串仍为url编码,这以后可能会出现问题。

  • 您知道哪个元数据条目引起了问题吗? (2认同)