存储名称为 unique/random 的文件
Store files with unique/random names
使用新的 Firebase API,您可以从客户端代码将文件上传到云存储中。 examples 假定文件名在上传期间是已知的或静态的:
// Create a root reference
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.jpg'
var mountainsRef = storageRef.child('mountains.jpg');
// Create a reference to 'images/mountains.jpg'
var mountainImagesRef = storageRef.child('images/mountains.jpg');
或
// File or Blob, assume the file is called rivers.jpg
var file = ...
// Upload the file to the path 'images/rivers.jpg'
// We can use the 'name' property on the File API to get our file name
var uploadTask = storageRef.child('images/' + file.name).put(file);
随着用户上传自己的文件,名称冲突将成为一个问题。您如何让 Firebase 创建一个文件名而不是自己定义它?数据库中是否有类似 push()
的功能来创建唯一存储引用?
这里是 Firebase 存储产品经理:
TL;DR:使用 UUID 生成器(在 Android (UUID) and iOS (NSUUID) they are built in, in JS you can use something like this: Create GUID / UUID in JavaScript?) 中,然后附加文件扩展名(如果要保留它)(将 file.name 拆分为 '. ' 并获取最后一段)
我们不知道开发人员想要哪个版本的独特文件(见下文),因为有很多很多用例,所以我们决定将选择权留给开发人员。
images/uuid/image.png // option 1: clean name, under a UUID "folder"
image/uuid.png // option 2: unique name, same extension
images/uuid // option 3: no extension
在我看来,这在我们的文档中解释是合理的,所以我将在内部提交一个错误来记录它:)
首先安装uuid - npm i uuid
然后像这样定义文件引用
import { v4 as uuidv4 } from "uuid";
const fileRef = storageRef.child(
`${uuidv4()}-${Put your file or image name here}`
);
之后,上传带有 fileRef
的文件
fileRef.put(Your file)
这是针对使用 dart 的人的解决方案
使用以下方法生成当前日期和时间戳:-
var time = DateTime.now().millisecondsSinceEpoch.toString();
现在使用以下方式将文件上传到 firebase 存储:-
await FirebaseStorage.instance.ref('images/$time.png').putFile(yourfile);
您甚至可以通过以下方式获得可下载的 url:-
var url = await FirebaseStorage.instance.ref('images/$time.png').getDownloadURL();
在 Android (Kotlin) 中,我通过将用户 UID 与自 1970 年以来的毫秒数相结合来解决:
val ref = storage.reference.child("images/${auth.currentUser!!.uid}-${System.currentTimeMillis()}")
使用新的 Firebase API,您可以从客户端代码将文件上传到云存储中。 examples 假定文件名在上传期间是已知的或静态的:
// Create a root reference
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.jpg'
var mountainsRef = storageRef.child('mountains.jpg');
// Create a reference to 'images/mountains.jpg'
var mountainImagesRef = storageRef.child('images/mountains.jpg');
或
// File or Blob, assume the file is called rivers.jpg
var file = ...
// Upload the file to the path 'images/rivers.jpg'
// We can use the 'name' property on the File API to get our file name
var uploadTask = storageRef.child('images/' + file.name).put(file);
随着用户上传自己的文件,名称冲突将成为一个问题。您如何让 Firebase 创建一个文件名而不是自己定义它?数据库中是否有类似 push()
的功能来创建唯一存储引用?
这里是 Firebase 存储产品经理:
TL;DR:使用 UUID 生成器(在 Android (UUID) and iOS (NSUUID) they are built in, in JS you can use something like this: Create GUID / UUID in JavaScript?) 中,然后附加文件扩展名(如果要保留它)(将 file.name 拆分为 '. ' 并获取最后一段)
我们不知道开发人员想要哪个版本的独特文件(见下文),因为有很多很多用例,所以我们决定将选择权留给开发人员。
images/uuid/image.png // option 1: clean name, under a UUID "folder"
image/uuid.png // option 2: unique name, same extension
images/uuid // option 3: no extension
在我看来,这在我们的文档中解释是合理的,所以我将在内部提交一个错误来记录它:)
首先安装uuid - npm i uuid
然后像这样定义文件引用
import { v4 as uuidv4 } from "uuid";
const fileRef = storageRef.child(
`${uuidv4()}-${Put your file or image name here}`
);
之后,上传带有 fileRef
fileRef.put(Your file)
这是针对使用 dart 的人的解决方案
使用以下方法生成当前日期和时间戳:-
var time = DateTime.now().millisecondsSinceEpoch.toString();
现在使用以下方式将文件上传到 firebase 存储:-
await FirebaseStorage.instance.ref('images/$time.png').putFile(yourfile);
您甚至可以通过以下方式获得可下载的 url:-
var url = await FirebaseStorage.instance.ref('images/$time.png').getDownloadURL();
在 Android (Kotlin) 中,我通过将用户 UID 与自 1970 年以来的毫秒数相结合来解决:
val ref = storage.reference.child("images/${auth.currentUser!!.uid}-${System.currentTimeMillis()}")