问题
Is there a way to set the storage tier to “cool” at the blob level when uploading a block blob to Azure Storage using the Java SDK? The closest thing I can find is setStandardBlobTier() on BlobProperties, which is a protected method, so it can't be accessed.
回答1:
The class CloudBlockBlob now has two uploadStandardBlobTier() methods which can be used to set the blob tier on a block blob on a standard storage account. e.g.
cloudBlockBlob.uploadStandardBlobTier(StandardBlobTier.COOL);
回答2:
I searched the Azure Storage java SDK source code and setStandardBlobTier()
method is protected method. I tried to create subclass of the BlobProperties
Class and overwrite the setStandardBlobTier()
method, but BlobProperties
Class was decorated with the final
keyword.
I also searched Azure Storage c# SDK, only get
method was found.
It seems you cannot set blob tier via sdk , however you can set tier via rest api.
You could refer to the sample code as below:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class SetBlobTier {
private static final String account = "***";
private static final String key = "***";
public static void main(String args[]) throws Exception {
String urlString = "https://" + account + ".blob.core.windows.net/***/***?comp=tier";
HttpURLConnection connection = (HttpURLConnection) (new URL(urlString)).openConnection();
getFileRequest(connection, account, key);
// connection.connect();
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
out.flush();
out.close();
System.out.println("Response message : " + connection.getResponseMessage());
System.out.println("Response code : " + connection.getResponseCode());
BufferedReader br = null;
if (connection.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}
public static void getFileRequest(HttpURLConnection request, String account, String key)
throws Exception {
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = fmt.format(Calendar.getInstance().getTime()) + " GMT";
String stringToSign = "PUT\n" + "\n" // content encoding
+ "\n" // content language
+ "\n"// content length
+ "\n" // content md5
+ "\n" // content type
+ "\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "\n"
+ "x-ms-date:" + date + "\n"
+ "x-ms-version:2015-02-21"+"\n" // headers
+ "/" + account + request.getURL().getPath(); // resources
System.out.println("stringToSign : " + stringToSign);
String auth = getAuthenticationString(stringToSign);
request.setRequestMethod("PUT");
request.setRequestProperty("x-ms-date", date);
request.setRequestProperty("x-ms-version", "2015-02-21");
request.setRequestProperty("x-ms-access-tier", "Archive");
request.setRequestProperty("Authorization", auth);
}
private static String getAuthenticationString(String stringToSign) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(Base64.decode(key), "HmacSHA256"));
String authKey = new String(Base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8"))));
String auth = "SharedKey " + account + ":" + authKey;
return auth;
}
}
Hope it helps you.
来源:https://stackoverflow.com/questions/48870035/azure-java-sdk-set-block-blob-to-cool-storage-tier-on-upload