[英]Upload file on firebase storage using the firebase admin sdk (java)
[英]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.