如何使用com.microsoft.azure.storage.blob.CloudBlockBlob的upload()

时间:2016-08-01 08:46:25

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

我正在开发一个代码,我需要将文件数据上传到azure storage blob。我的要求是以加密格式上传这些数据,因此我使用的是azure密钥保险库。

final String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=abc;AccountKey=pqr+lov=="; 
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
com.microsoft.azure.storage.blob.CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("plmcontainer2");
container.createIfNotExists();
String filePath = "C:\\STSWorkspace\\PLMSubscriberMS\\Payload.xml";
com.microsoft.azure.storage.blob.CloudBlockBlob blob = container.getBlockBlobReference("Payload4.xml");
java.io.File source = new java.io.File(filePath);
java.io.FileInputStream fileInputStream=new java.io.FileInputStream(source);
// blob.upload(fileInputStream, source.length());


//encryption code

// Create the IKey used for encryption.
RsaKey key = new RsaKey("private:key1" /* key identifier */);


// Create the encryption policy to be used for upload and download.
BlobEncryptionPolicy policy = new BlobEncryptionPolicy(key, null);

// Set the encryption policy on the request options.
BlobRequestOptions options = new BlobRequestOptions();

options.setEncryptionPolicy(policy);

AccessCondition accessCondition = null;
operationContext opContext = null;
// Upload the encrypted contents to the blob.
blob.upload(fileInputStream, source.length(), null, options, null); //here is exception

在最后一行,我得到一个例外, 如果我将其更改为blob.upload(fileInputStream, source.length());

然后将数据上传到博客中,但是以纯文本形式上传。 如何使用blob.upload(fileInputStream, source.length(), null, options, null);我应该在null的位置放置什么。

异常

Exception in thread "main" 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)
    at com.microsoft.azure.storage.blob.BlobEncryptionPolicy.createAndSetEncryptionContext(BlobEncryptionPolicy.java:305)
    at com.microsoft.azure.storage.blob.CloudBlockBlob.openOutputStream(CloudBlockBlob.java:575)
    at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:715)
    at com.encrypt.blob.BlobEncryption.main(BlobEncryption.java:55)
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026)
    at javax.crypto.Cipher.implInit(Cipher.java:801)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
    at javax.crypto.Cipher.init(Cipher.java:1249)
    at javax.crypto.Cipher.init(Cipher.java:1186)
    at com.microsoft.azure.storage.blob.BlobEncryptionPolicy.createAndSetEncryptionContext(BlobEncryptionPolicy.java:288)
    ... 3 more

1 个答案:

答案 0 :(得分:0)

@AnandDeshmukh,我转载了这个问题。似乎问题是由于没有为您当前的JRE或JDK安装Java Cryptography Extension(JCE)Unlimited Strength Jurisdiction Policy Files而引起的。有一个可以参考的SO线程Java Security: Illegal key size or default parameters?

我的Java环境是JDK8u101。当我下载Java 8的jce文件而不是原始文件时,异常消失了。

请下载相关版本的JCE文件以解决问题。