简体   繁体   中英

XML is not storing completely in Azure Storage Account via java

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 stored file. How to store the all lines of xml into azure storage using java?

See the below screen shot for more info,

Actual:

在此处输入图像描述

Expected:

在此处输入图像描述

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

在此处输入图像描述

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.");
        }
    }

}

在此处输入图像描述 FIle which i have uploaded from local

在此处输入图像描述

Content of File in Azure Storage Account

在此处输入图像描述

I have called below flush and close methods on FileWriter. Its started working now. 在此处输入图像描述

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