I'm able to upload files on Google Cloud Storage using Firebase Admin SDK. However, I'm unable to set the correct contentType for the file and the uploaded file defaults to application/octet-stream
. I'm unable to any documentations for setting the correct contentType/metadata using Java Admin SDK. How I can change the default contentType?
Here's the code snippet on how I upload the file.
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);
Turns out that I need to use Google Cloud Storage API . The docs for Firebase Cloud Storage for Admin SDK is barren. I hope that the docs were clearer.
Here's the snippet on how I modified the contentType for my file upload. This changed the contentType from application/octet-stream
to 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);
}
I'm still unable to download the uploaded file on Firebase Storage dashboard , but the uploaded file can be downloaded through Google Cloud Storage dashboard .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.