Azure Blob的加密

时间:2016-09-10 10:32:26

标签: java azure encryption azure-storage-blobs

我想以加密格式将数据存储在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]`

1 个答案:

答案 0 :(得分:0)

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

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