簡體   English   中英

Firebase Cloud Storage Java Admin SDK 上傳帶有 contentType 的文件

[英]Firebase Cloud Storage Java Admin SDK upload file with contentType

我可以使用 Firebase Admin SDK 在 Google Cloud Storage 上上傳文件。 但是,我無法為文件設置正確的 contentType 並且上傳的文件默認為application/octet-stream 我無法使用 Java Admin SDK 設置正確的 contentType/元數據的任何文檔。 如何更改默認的 contentType?

這是關於我如何上傳文件的代碼片段。

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String timestamp = dateFormat.format(date);
StorageClient storageClient = StorageClient.getInstance();
Bucket storageBucket = storageClient.bucket();

// Not sure if I'm doing this correctly
BlobInfo blobInfo = BlobInfo.newBuilder(storageBucket.getName(), "filename")
        .setContentType("vnd.ms-excel").build();

// File upload task
storageClient.bucket().create(backupFolderPath + fileNamePrefix + "_" + timestamp + ".csv", file);

原來我需要使用Google Cloud Storage API Firebase Cloud Storage for Admin SDK 的文檔很貧乏。 我希望文檔更清楚。

這是我如何修改文件上傳的 contentType 的片段。 這將 contentType 從application/octet-stream更改為text/csv

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String timestamp = dateFormat.format(date);
StorageClient storageClient = StorageClient.getInstance();

try {
  String bucketName = "<YOUR_BUCKET_NAME>";
  String objectName = backupFolderPath + fileNamePrefix + "_" + timestamp + ".csv";
  BlobId blobId = BlobId.of(bucketName, objectName);
  
  // Configure BlobInfo
  BlobInfo blob = BlobInfo.newBuilder(blobId)
      .setContentType("text/csv")
      .setContentEncoding("utf-8").build();

  // Use fetch `storage` from the bucket
  // https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-code-sample
  storageClient.bucket().getStorage().create(blob, 
      Files.readAllBytes(Paths.get("backup/accounts.csv")));
} catch (IOException e) {
  logger.error(">>>>> Message from Scheduled Job " + e);
}

我仍然無法在Firebase Storage 儀表板上下載上傳的文件,但可以通過Google Cloud Storage 儀表板下載上傳的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM