XML 未通过 java 完全存储在 Azure 存储帐户中

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

我正在使用上面的代码将 xml 文件存储到 azure storage.The 我面临的问题是 azure 存储文件中缺少 xml 的最后几行。如何使用 java?

将 xml 的所有行存储到 azure 存储中

有关详细信息,请参见下面的屏幕截图,

实际:

预计:

看来您是在制作自己的连接字符串,如果这样的话,它不会那样工作。请粘贴门户本身的连接字符串。

代码:

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

}

我从本地上传的文件

Azure 存储帐户中的文件内容

我在 FileWriter 上调用了以下刷新和关闭方法。它现在开始工作了。