如何使用 firebase 存储移动文件?
How to move files with firebase storage?
有没有办法用 firebase.storage() 移动文件?
示例:
user1/public/image.jpg 到 user1/private/image.jpg
没有移动到其他位置的方法,您可以下载然后放到其他参考并删除以前的位置。
由于 Firebase 存储由 Google 云存储支持,您可以使用 GCS 的 rewrite
API (docs) or gsutil mv
(docs)。
此外,GCloud 节点中 move
(docs) 的示例如下:
var bucket = gcs.bucket('my-bucket');
var file = bucket.file('my-image.png');
var newLocation = 'gs://another-bucket/my-image-new.png';
file.move(newLocation, function(err, destinationFile, apiResponse) {
// `my-bucket` no longer contains:
// - "my-image.png"
//
// `another-bucket` now contains:
// - "my-image-new.png"
// `destinationFile` is an instance of a File object that refers to your
// new file.
});
我编写了一个 JavaScript 函数,仅使用 firebase 存储 API。
编码愉快!
/**
* Moves a file in firebase storage from its current location to the destination
* returns the status object for the moved file.
* @param {String} currentPath The path to the existing file from storage root
* @param {String} destinationPath The desired pathe for the existing file after storage
*/
function moveFirebaseFile(currentPath, destinationPath) {
let oldRef = storage.ref().child(currentPath)
oldRef.getDownloadURL().then(url => {
fetch(url).then(htmlReturn => {
let fileArray = new Uint8Array()
const reader = htmlReturn.body.getReader()
//get the reader that reads the readable stream of data
reader
.read()
.then(function appendStreamChunk({ done, value }) {
//If the reader doesn't return "done = true" append the chunk that was returned to us
// rinse and repeat until it is done.
if (value) {
fileArray = mergeTypedArrays(fileArray, value)
}
if (done) {
console.log(fileArray)
return fileArray
} else {
// "Readout not complete, reading next chunk"
return reader.read().then(appendStreamChunk)
}
})
.then(file => {
//Write the file to the new storage place
let status = storage
.ref()
.child(destinationPath)
.put(file)
//Remove the old reference
oldRef.delete()
return status
})
})
})
}
自 2022 年 3 月 29 日起,可以通过以下方式直接调用“移动”功能:
import * as functions from "firebase-functions";
import {getStorage} from "firebase-admin/storage";
export const triggerStorage = functions
.storage
.object()
.onFinalize(async (object) => {
const bucket = getStorage().bucket(object.bucket);
const destPath = "user1/private/image.jpg";
await bucket.file(object.name).move(destPath);
});
有没有办法用 firebase.storage() 移动文件?
示例: user1/public/image.jpg 到 user1/private/image.jpg
没有移动到其他位置的方法,您可以下载然后放到其他参考并删除以前的位置。
由于 Firebase 存储由 Google 云存储支持,您可以使用 GCS 的 rewrite
API (docs) or gsutil mv
(docs)。
此外,GCloud 节点中 move
(docs) 的示例如下:
var bucket = gcs.bucket('my-bucket');
var file = bucket.file('my-image.png');
var newLocation = 'gs://another-bucket/my-image-new.png';
file.move(newLocation, function(err, destinationFile, apiResponse) {
// `my-bucket` no longer contains:
// - "my-image.png"
//
// `another-bucket` now contains:
// - "my-image-new.png"
// `destinationFile` is an instance of a File object that refers to your
// new file.
});
我编写了一个 JavaScript 函数,仅使用 firebase 存储 API。
编码愉快!
/**
* Moves a file in firebase storage from its current location to the destination
* returns the status object for the moved file.
* @param {String} currentPath The path to the existing file from storage root
* @param {String} destinationPath The desired pathe for the existing file after storage
*/
function moveFirebaseFile(currentPath, destinationPath) {
let oldRef = storage.ref().child(currentPath)
oldRef.getDownloadURL().then(url => {
fetch(url).then(htmlReturn => {
let fileArray = new Uint8Array()
const reader = htmlReturn.body.getReader()
//get the reader that reads the readable stream of data
reader
.read()
.then(function appendStreamChunk({ done, value }) {
//If the reader doesn't return "done = true" append the chunk that was returned to us
// rinse and repeat until it is done.
if (value) {
fileArray = mergeTypedArrays(fileArray, value)
}
if (done) {
console.log(fileArray)
return fileArray
} else {
// "Readout not complete, reading next chunk"
return reader.read().then(appendStreamChunk)
}
})
.then(file => {
//Write the file to the new storage place
let status = storage
.ref()
.child(destinationPath)
.put(file)
//Remove the old reference
oldRef.delete()
return status
})
})
})
}
自 2022 年 3 月 29 日起,可以通过以下方式直接调用“移动”功能:
import * as functions from "firebase-functions";
import {getStorage} from "firebase-admin/storage";
export const triggerStorage = functions
.storage
.object()
.onFinalize(async (object) => {
const bucket = getStorage().bucket(object.bucket);
const destPath = "user1/private/image.jpg";
await bucket.file(object.name).move(destPath);
});