Azure Java SDK - 在上载时将块blob设置为冷却存储层

时间:2018-02-19 16:14:22

标签: azure-storage-blobs azure-java-sdk

使用Java SDK将块blob上传到Azure存储时,有没有办法将存储层设置为blob级别的“酷”?我能找到的最接近的东西是BlobProperties上的setStandardBlobTier(),它是一种受保护的方法,因此无法访问它。

2 个答案:

答案 0 :(得分:1)

我搜索了Azure Storage java SDK source codesetStandardBlobTier()方法是受保护的方法。我尝试创建BlobProperties类的子类并覆盖setStandardBlobTier()方法,但BlobProperties类使用final关键字进行修饰。

我还搜索了Azure Storage c# SDK,但只发现了get个方法。 您似乎无法通过sdk设置blob层,但是您可以通过rest api设置层。

您可以参考以下示例代码:

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;
    }
}

希望它对你有所帮助。

答案 1 :(得分:1)

CloudBlockBlob现在具有两个uploadStandardBlobTier()方法,可用于在标准存储帐户的块blob上设置blob层。例如

cloudBlockBlob.uploadStandardBlobTier(StandardBlobTier.COOL);