3

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

4

2 回答 2

2

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

cloudBlockBlob.uploadStandardBlobTier(StandardBlobTier.COOL);
于 2018-09-21T17:36:55.440 回答
1

我搜索了Azure Storage java SDK 源代码setStandardBlobTier()方法是受保护的方法。我试图创建 Class 的子BlobProperties类并覆盖该setStandardBlobTier()方法,但BlobPropertiesClass 用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;
    }
}

希望它可以帮助你。

于 2018-02-21T07:02:51.660 回答