【问题标题】:Encryption For Azure BlobAzure Blob 的加密
【发布时间】:2017-01-18 09:45:03
【问题描述】:

我想以加密格式将数据存储在 azure blob 中。我使用This 作为参考。

RsaKey key = new RsaKey("private:key1" /* key identifier */);
BlobEncryptionPolicy policy = new BlobEncryptionPolicy(null, null);
BlobRequestOptions options = new BlobRequestOptions();
options.setEncryptionPolicy(policy);
boolean tableExistsOrNOt = true;
if (blobContainer == null) {
    tableExistsOrNOt = blobContainer.createIfNotExists();
}
if (tableExistsOrNOt) {
    CloudBlockBlob blob = blobContainer.getBlockBlobReference(xml.get(ecnNumberKey).toString());
    try {
        InputStream inputStream = new ByteArrayInputStream(
        xml.get(xmlKey).toString().getBytes(StandardCharsets.UTF_8));
        //  blob.upload(inputStream, inputStream.available());
        //  blob.upload(inputStream, inputStream.available(), null, options, null);
        blob.upload(inputStream, inputStream.available(),null, options, null);
    } catch (Exception e) {
        LOG.error("Exception while inserting xml to blob in PLMSubscriberMSDaoImpl.insertPayloadXMLToBlob", e);
        LOG.info("#####Ending PLMSubscriberMSDaoImpl.insertPayloadXMLToBlob#####");
        return false;
    }
}

在此我在blob.upload(inputStream, inputStream.available(),null, options, null); 收到关注异常 堆栈跟踪如下:

`com.microsoft.azure.storage.StorageException: A Client side exception occurred, please check the inner exception for details
    at com.microsoft.azure.storage.StorageException.translateClientException(StorageException.java:42) ~[azure-storage-4.3.0.jar:na]
    at com.microsoft.azure.storage.blob.BlobEncryptionPolicy.createAndSetEncryptionContext(BlobEncryptionPolicy.java:305) ~[azure-storage-4.3.0.jar:na]
    at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:669) ~[azure-storage-4.3.0.jar:na]
    at com.jci.subscriber.dao.PLMSubscriberMSDaoImpl.insertPayloadXMLToBlob(PLMSubscriberMSDaoImpl.java:69) ~[classes/:na]
    at com.jci.subscriber.service.PLMSubscriberMSServiceImpl.azureMessageSubscriber(PLMSubscriberMSServiceImpl.java:174) [classes/:na]
    at com.jci.subscriber.PLMSubscriberMSApplication.azureSBXMLPost(PLMSubscriberMSApplication.java:78) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]`

【问题讨论】:

  • 根据您引用的文章,RsaKey 位于 azure-keyvault-extensions
  • @MartinParkin 它在blob.upload(inputStream, inputStream.available(),null, options, null); 给出一个例外
  • 刚刚顺便提到了静态加密现在作为一项功能提供(预览版):azure.microsoft.com/en-us/documentation/articles/…

标签: java azure encryption azure-blob-storage


【解决方案1】:

@eddie,好像是使用inputStream.available()作为上传内容大小的参数引起的,请尝试使用下面的代码修复问题。

try {
    byte[] buf = xml.get(xmlKey).toString().getBytes(StandardCharsets.UTF_8);
    int size = buf.length;
    InputStream inputStream = new ByteArrayInputStream(buf);
    //  blob.upload(inputStream, inputStream.available());
    //  blob.upload(inputStream, inputStream.available(), null, options, null);
    blob.upload(inputStream, size,null, options, null);
} catch (Exception e) {
    LOG.error("Exception while inserting xml to blob in PLMSubscriberMSDaoImpl.insertPayloadXMLToBlob", e);
    LOG.info("#####Ending PLMSubscriberMSDaoImpl.insertPayloadXMLToBlob#####");
    return false;
}

作为参考,有一个sample 用于 Microsoft Azure 存储的 Java 客户端加密,您可以参考。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2017-10-10
    • 2021-10-20
    • 1970-01-01
    • 2018-08-17
    • 2021-01-11
    • 2018-01-01
    • 2021-11-30
    • 2016-09-05
    • 2018-02-24
    相关资源
    最近更新 更多