简体   繁体   English

使用Rest API通过versionId删除Amazon s3对象

[英]Delete amazon s3 object by versionId using rest API

I am trying to delete amazon s3 object using rest API but not getting any success. 我正在尝试使用rest API删除Amazon s3对象,但未获得任何成功。 I have created the URL(signed url) at server side using java and then made XHR request to that URL at client side(ie from browser). 我已经使用Java在服务器端创建了URL(签名URL),然后在客户端(即从浏览器)向该URL发出了XHR请求。

Java code that i have used to sign the url: 我用来签署网址的Java代码:

    public static String getSignedURL(String fileName, int fileOwnerId, String versionId){
    Date expiration = new Date();
    long milliSeconds = expiration.getTime();
    milliSeconds += 1000 * 60 * 10; // Add 10 minutes.
    long seconds = (milliSeconds)/1000L;
    String URL = null;

    try {
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        String canonicalizedResource = "/"+AWS_BUCKET_NAME+"/" + fileOwnerId + "/" + encodedFileName;
        String stringToSign = "DELETE\n\n\n" + seconds + "\n" + canonicalizedResource +"?versionId="+versionId;
        byte[] keyBytes = AWS_SECRET_API_KEY.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);

        byte[] digest = mac.doFinal(stringToSign.getBytes());
        byte[] base64bytes = Base64.encodeBase64(digest);
        String signedString = new String(base64bytes, "UTF-8");

        String signature = URLEncoder.encode(signedString, "UTF-8");

        URL = "https://"+AWS_BUCKET_NAME+".s3.amazonaws.com/" + fileOwnerId +
                "/" + encodedFileName +"?versionId="+versionId +"&Expires=" + seconds+"&AWSAccessKeyId=" +
                AWS_ACCESS_KEY + "&Signature=" + signature;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException nsae) {

    } catch (InvalidKeyException ike) {

    }

    System.out.println("URL IS :"+URL);
    return URL;
}

And at client side: 在客户端:

    var xhr = new XMLHttpRequest();

    xhr.addEventListener("load", deleteComplete, false);

    xhr.open('DELETE', URL_GENERATED_FROM_SERVER, true); 

    xhr.setRequestHeader ("Access-Control-Allow-Origin", "*");

    xhr.send();

Using this code for downloading an object from amazon s3 bucket works fine by replacing 'DELETE' request with 'GET'. 使用此代码从amazon s3存储桶下载对象可以很好地解决此问题,方法是将“ DELETE”请求替换为“ GET”。 But delete is not working. 但是删除不起作用。 I have searched a lot but there is very less help available for rest API. 我进行了很多搜索,但其余API的可用帮助却很少。

Finally, i integrated the aws sdk to delete the object from amazon s3 bucket and it works like lightning. 最后,我集成了aws sdk以便从Amazon s3存储桶中删除该对象,其工作方式类似于闪电。 But unable to get help doing it with rest API. 但是无法获得其他API的帮助。 So now i have used rest API for uploading and downloading and the sdk for deleting an object. 因此,现在我已使用rest API进行上传和下载,并使用sdk删除对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM