【问题标题】:Azure Java SDK - set block blob to cool storage tier on uploadAzure Java SDK - 在上传时将块 blob 设置为冷却存储层
【发布时间】:2018-09-22 01:36:55
【问题描述】:

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

【问题讨论】:

  • 嗨,有更新吗?
  • 我也没有找到适用于 Java SDK 的解决方案,而且我认为目前还没有。我会赞成你的答案,因为它确实通过 REST API 提供了一种解决方法,但我宁愿等待 Java SDK 赶上来,然后再实施某些东西,因为推迟实施这个功能对我们来说不是什么大问题。跨度>
  • 是的,我完全同意。我注意到目前没有任何 SDK 支持修改 StandardTier 属性,而不仅仅是 Java SDK。我建议你提交反馈:feedback.azure.com/forums/34192--general-feedback.
  • 如果有任何进展,请与我分享。谢谢。

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


【解决方案1】:

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

cloudBlockBlob.uploadStandardBlobTier(StandardBlobTier.COOL);

【讨论】:

    【解决方案2】:

    我搜索了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;
        }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 2020-05-02
      • 2012-01-28
      • 2020-08-12
      • 2021-11-08
      • 2018-08-13
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      相关资源
      最近更新 更多