0
我想刪除亞馬遜s3對象使用其餘的API,但沒有得到任何成功。我使用java在服務器端創建了URL(簽名url),然後在客戶端(即從瀏覽器)向該URL發出XHR請求。使用rest1刪除亞馬遜s3對象由versionId API
,我已經使用並登錄的網址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;
}
而在客戶端:
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();
使用此代碼從Amazon S3存儲下載對象,通過替換'工作正常用'GET'刪除'請求。但刪除不起作用。我搜索了很多,但其餘API的幫助非常少。
爲什麼要將fileOwnerId添加到stringToSign? – okwap
感謝您的回覆@okwap。我補充說,因爲這是我在s3上保存文件的方式。像bucketname/username/file.txt – Arbind