从Java对象生成CSV并移至Azure存储而无需中间位置

问题描述 投票:0回答:1

是否可以从Java对象创建CSV文件,并将其移动到Azure存储而不使用临时位置?

java csv azure storage csv-write-stream
1个回答
1
投票

根据您的描述,您似乎想要上传CSV文件而不占用本地空间。因此,我建议您使用流将CSV文件上载到Azure文件存储。

请参考以下示例代码:

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.file.CloudFile;
import com.microsoft.azure.storage.file.CloudFileClient;
import com.microsoft.azure.storage.file.CloudFileDirectory;
import com.microsoft.azure.storage.file.CloudFileShare;
import com.microsoft.azure.storage.StorageCredentials;
import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringBufferInputStream;

public class UploadCSV {

    // Configure the connection-string with your values
    public static final String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
                    "AccountName=<storage account name>;" +
                    "AccountKey=<storage key>";


    public static void main(String[] args) {
        try {
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the Azure Files client.
            CloudFileClient fileClient = storageAccount.createCloudFileClient();

            StorageCredentials sc = fileClient.getCredentials();

            // Get a reference to the file share
            CloudFileShare share = fileClient.getShareReference("test");

            //Get a reference to the root directory for the share.
            CloudFileDirectory rootDir = share.getRootDirectoryReference();

            //Get a reference to the file you want to download
            CloudFile file = rootDir.getFileReference("test.csv");

            file.upload( new StringBufferInputStream("aaa"),"aaa".length());

            System.out.println("upload success");

        } catch (Exception e) {
            // Output the stack trace.
            e.printStackTrace();
        }
    }
}

然后我成功将文件上传到帐户。

enter image description here

你也可以参考线程:

1.Can I upload a stream to Azure blob storage without specifying its length upfront?

2.Upload blob in Azure using BlobOutputStream

希望它能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.