从S3存储桶中删除多个对象时出现SignatureDoesNotMatch错误

时间:2018-04-12 06:25:35

标签: java amazon-s3 httpurlconnection

我添加了从S3删除多个对象的代码。 POST删除请求返回403状态。我在遗留代码中添加此代码,遗憾的是无法更改为更改AWS S3库。无法弄清楚我是否缺少标题或逻辑是否错误。

public Response deleteMultipleObjects(Delete object) throws MalformedURLException, IOException, StorageException {

    HttpURLConnection request = null;

    try(ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(out)) {

        os.writeObject(object);

        Calendar now = Calendar.getInstance();

        Map<String, List<String>> headers = new HashMap<>();

        headers.put(AWSAuthConnection.CONTENT_TYPE_HEADER, Arrays.asList("application/octet-stream"));
        headers.put(AWSAuthConnection.CONTENT_LENGTH_HEADER, Arrays.asList(Integer.toString(out.toByteArray().length)));
        headers.put(AWSAuthConnection.AMAZON_DATE_HEADER, Arrays.asList(convertDateToString(now, AWSAuthConnection.TIMESTAMP_FORMAT, AWSAuthConnection.TIME_ZONE_UTC)));
        headers.put(AWSAuthConnection.AMAZON_CONTENT_SHA256_HEADER, Arrays.asList(buildHash(out.toByteArray(), SHA256_HASH, EncodingType.HEX)));
        headers.put(AWSAuthConnection.CONTENT_MD5_HEADER, Arrays.asList(buildHash(out.toByteArray(), AWSAuthConnection.MD5_HASH, AWSAuthConnection.EncodingType.BASE64)));

        request = makeRequest("POST",  bucket+"/?delete", headers, null);

        request.setDoOutput(true);

        request.getOutputStream().write(object == null ? new byte[]{} : out.toByteArray());

        return new Response(request);
    }

}

private String buildHash(final byte[] contentToHash, final String hashMethod, final AWSAuthConnection.EncodingType encodingType) throws StorageException{
    try {
        MessageDigest digest = MessageDigest.getInstance(hashMethod);
        byte[] contentHash = digest.digest(contentToHash);
        if (encodingType == AWSAuthConnection.EncodingType.HEX) {
            return Hex.encodeHexString(contentHash);
        } else {
            return Base64.encodeBase64String(contentHash);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new StorageException(StorageException.ERROR_INVALID_STORAGE_TYPE, e);
    }
}

private HttpURLConnection makeRequest(String method, String resource, Map<String, List<String>> headers, StorageObject object) throws MalformedURLException, IOException {
    URL url = makeURL(resource);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(method);

    connection.setUseCaches(true);

    addHeaders(connection, headers);
    if (object != null) addMetadataHeaders(connection, object.metadata);
    addAuthHeader(connection, method, resource);

    return connection;
}

private void addAuthHeader(HttpURLConnection connection, String method, String resource) {    

    String canonicalString =
            Utils.makeCanonicalString(method, resource, connection.getRequestProperties());


    String encodedCanonical = Utils.encode(this.info, canonicalString, false);


    connection.setRequestProperty("Authorization", "AWS " + this.info + ":" + encodedCanonical);
}

S3出错:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>.......</Error>

以前有人遇到过这个问题吗?如果您知道解决方法,请帮助

0 个答案:

没有答案