从 Java 对象生成 CSV 并在没有中间位置的情况下移动到 Azure 存储

Generate CSV from Java object and move to Azure Storage without intermediate location

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

根据您的描述,您似乎想上传一个 CSV 文件而不占用您的本地 space。因此,我建议您使用流将 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();
        }
    }
}

然后我就成功上传文件到账户了。

您还可以参考以下话题:

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

2.Upload blob in Azure using BlobOutputStream

希望对你有帮助。