问题
I am trying to copy or move files from the root in Firebase Storage to a folder. More specifically, from users/displayName/uid/ to users/displayName/uid/somefolder. I have read that there is no method in the Firebase Storage API to make a copy of a file that you've already uploaded and that you will have to download the data and re-upload it. I could not find any sample codes, however. Therefore, I have written the following code to try to accomplish that but it does not work. See the error below to find out why.
Here's the code I have written:
const listRef = firebase.storage().ref(`users/${this.state.displayName}/${this.state.uid}`)
listRef.listAll().then((res) => {
res.items.forEach((itemRef) => {
firebase.storage().ref(`users/${this.state.displayName}/${this.state.uid}/somefolder`).put(itemRef)
});
}
).catch(function (error) {
console.log(error)
});
So, this code does not work and nothing happens. No folder is created. Here's the error that comes up:
FirebaseStorageError {code_: "storage/invalid-argument", message_: "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.", serverResponse_: null, name_: "FirebaseError"}
code: (...)
code_: "storage/invalid-argument"
message: (...)
message_: "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File."
name: (...)
name_: "FirebaseError"
serverResponse: (...)
serverResponse_: null
__proto__: Object
By all appearances, the put
method expects a file or blob and not the items that you get with the listAll()
method.
Any ideas on how to fix this issue and move or copy a file to a folder successfully? Real code samples will be appreciated. Thank you.
回答1:
You can't pass a reference put()
. According to the API documentation, there are three different things you can pass: a Blob, an Uint8array, or an ArrayBuffer.
The problem, however, is that the javascript web client SDK doesn't give you a way of directly downloading data in any of those formats. You're going to have to figure out how to do that yourself, perhaps by getting a download URL, or offloading that work to a backend that can use a server SDK to copy data around.
来源:https://stackoverflow.com/questions/62865814/why-does-my-code-that-attempts-to-copy-files-from-the-root-directory-of-firebase