Home > Software engineering >  xml is not storing completely in azure storage account via java
xml is not storing completely in azure storage account via java

Time:02-16

public void createAzureBlob(File file) {
//        System.out.println("AccountName : " configuration.getAccountName());
        String storageConnectionString =
                "DefaultEndpointsProtocol=https;"  
                        "AccountName="   "csb1003"   ";"   "AccountKey=sVQZOFBU0U/";

        File sourceFile = file;
        File downloadedFile = null;

        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
//            container = blobClient.getContainerReference("quickstartcontainer5");
            container = blobClient.getContainerReference("transfer-to-ckb-container");

            // Create the container if it does not exist with public access.
//            System.out.println("Creating container: "   container.getName());
            container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER,
                    new BlobRequestOptions(),
                    new OperationContext());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and
            // haven't changed the blob we're interested in, so we can reuse it.
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = new File(sourceFile.getParentFile(), "downloadedFile.xml");
            blob.downloadToFile(downloadedFile.getAbsolutePath());
        } catch (StorageException ex) {
            System.out.println(String.format("Error returned from the service. Http code: %d and error code: %s",
                    ex.getHttpStatusCode(),
                    ex.getErrorCode()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The program has completed successfully.");
        }
    }

I am using above code to store the xml files into azure storage.The problem i am facing is last few lines of xml are missing in the azure storaged file. How to store the all lines of xml into azure storage using java? See the below screen shot for more info,

Actual :

enter image description here

Expected :

enter image description here

CodePudding user response:

Seems you are making your own connection string if so it won't work like that. Please paste the connection string from portal itself.

enter image description here

Code :

package com.company;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

import java.io.File;

public class StorageUpload {
    public static void main(String[] args) {
        File file1 = new File("C:\\Users\\v-rash18\\Downloads\\SignUpOrSignin.xml");
        createAzureBlob(file1);
    }


    public static void createAzureBlob(File file) {
//        System.out.println("AccountName : " configuration.getAccountName());
        String storageConnectionString ="Connection String";
        File sourceFile = file;
        File downloadedFile = null;

        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
//          container = blobClient.getContainerReference("dscconffiles");
            container = blobClient.getContainerReference("dscconffiles");

            // Create the container if it does not exist with public access.
//            System.out.println("Creating container: "   container.getName());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and
            // haven't changed the blob we're interested in, so we can reuse it.
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = new File(sourceFile.getParentFile(), "downloadedFile.xml");
            blob.downloadToFile(downloadedFile.getAbsolutePath());
        } catch (StorageException ex) {
            System.out.println(String.format("Error returned from the service. Http code: %d and error code: %s",
                    ex.getHttpStatusCode(),
                    ex.getErrorCode()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The program has completed successfully.");
        }
    }

}

enter image description here FIle which i have uploaded from local

enter image description here

Content of File in Azure Storage Account

enter image description here

CodePudding user response:

I have called below flush and close methods on FileWriter. Its started working now. enter image description here

  • Related