简体   繁体   中英

How to use upload() of com.microsoft.azure.storage.blob.CloudBlockBlob

I am developing a code where I need to upload the file data into azure storage blob. My requirement is to upload this data in encrypted format,thus I am using azure key vault.

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

On the last line I am getting an exception, if I change it to blob.upload(fileInputStream, source.length());

then the data is uploaded into blog but in plain text. how do I use blob.upload(fileInputStream, source.length(), null, options, null); what should I place at the location of null.

Exception

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

@AnandDeshmukh, I reproduced the issue. It seems that the issue was caused by not installing Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your current JRE or JDK. There is an answered SO thread Java Security: Illegal key size or default parameters? which you can refer to.

My Java Environment is JDK8u101. When I downloaded the jce files for Java 8 instead of the original files, the exception disappeared.

Please download the related version of JCE files to solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM