简体   繁体   English

Firebase函数更改存储中上载文件的fileName

[英]Firebase functions change fileName for uploaded file in storage

Is there a way to change the fileName of an image with Firebase functions, without downloading and uploading it again? 是否可以使用Firebase函数更改图像的文件名,而无需再次下载和上传它?

I am using the onChange Listener to catch the actual upload, and I get all the data I need about the uploaded file, but I can't change any information without downloading it. 我正在使用onChange Listener来捕获实际的上载,并且获得了有关上载文件所需的所有数据,但是如果不下载则无法更改任何信息。

My current code: 我当前的代码:

const functions = require('firebase-functions');
const path = require('path');

exports.addTimeStamp = functions.storage.object().onChange(event => {
const object = event.data; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.

// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return;
}

if (!filePath.startsWith('deliveryNote/')) {
    console.log('This is not a delivery note.');
    return;
}

// Get the file name.
const fileName = path.basename(filePath);
console.log('filename: ' + fileName);
// Exit if the image is already a thumbnail.
if (fileName.startsWith('note_')) {
    console.log('Already modified');
    return;
}

// Exit if this is a move or deletion event.
if (resourceState === 'not_exists') {
    console.log('This is a deletion event.');
    return;
}

// Exit if file exists but is not new and is only being triggered
// because of a metadata change.
if (resourceState === 'exists' && metageneration > 1) {
    console.log('This is a metadata change event.');
    return;
}

/////////////////////////////////////////////////////////////
//Added folowing code thx to Doug Stevenson
const bucket = gcs.bucket(object.bucket);
var file = bucket.file(filePath);
console.log('filepath: ' + filePath);
console.log('filename: ' + fileName);
const dirname = path.dirname(filePath);
file.move(dirname + '/' + 'note_' + fileName, function(err, destinationFile, apiResponse) {
});
//////////////////////////////////////////////////////////////
});

To interact with files in a storage bucket, you can use the Google Cloud Storage Node SDK . 要与存储桶中的文件进行交互,可以使用Google Cloud Storage Node SDK Use it to obtain a File object that represents the file that changed, and use its move method to change its name. 使用它来获取表示已更改文件的File对象,并使用其move方法更改其名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Vuejs 获取 firebase 存储中上传文件的 url - Get url of uploaded file in firebase storage with Vuejs 通过 Firebase 函数下载文件到 Firebase 存储 - Download file to Firebase Storage via Firebase Functions firebase 存储文件名更改 - firebase storage file name change 上传相同文件名时忽略文件输入更改事件 - file input change event is ignored when same filename is uploaded 图片已上传但未在 firebase 存储中接收。 控制台显示文件上传成功。 但不显示在存储中 - image uploaded but not receive in firebase storage. console show file upload successfully. but not show in storage 如果将文件上传到Firebase存储,如何部署运行的Firebase功能? - How can I deploy a Firebase Function that runs if a file is uploaded to Firebase Storage? 如何获取上传的图片并将其存储在firebase存储中? - How to get an image uploaded and store it in the firebase storage? 如何从 Firebase 存储中获取图像文件名? - How to get image filename from Firebase Storage? 使用 Firebase Cloud Functions 访问 Firebase Storage 中的文件时出现问题,文件返回“[object Object]” - Problem with accessing file in Firebase Storage with Firebase Cloud Functions, file returns "[object Object] " 获取通过ajax上传的文件的文件名 - get filename of file uploaded through ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM