存储具有唯一/随机名称的文件 [英] Store files with unique/random names

查看:137
本文介绍了存储具有唯一/随机名称的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的Firebase API,您可以将文件从客户端代码上传到云端存储中。 示例假定在上传过程中文件名是已知的或者是静态的

  //创建一个根引用
var storageRef = firebase.storage()。ref();

//创建对'mountains.jpg'的引用
var mountainsRef = storageRef.child('mountains.jpg');

//创建一个'images / mountains.jpg'的引用
var mountainImagesRef = storageRef.child('images / mountains.jpg');

  // File或Blob,假设文件名为rivers.jpg 
var file = ...

//将文件上传到路径'images / rivers.jpg '
//我们可以在File API上使用'name'属性来获得文件名
var uploadTask = storageRef.child('images /'+ file.name).put(file);

用户上传自己的文件时,名称冲突将成为一个问题。如何让Firebase创建文件名而不是自己定义文件名?是否有像数据库中的 push()特性用于创建唯一的存储引用?

解决方案使用UUID生成器(在Android中)( UUID )和iOS( NSUUID )它们内置在JS中,你可以使用这样的事情:),然后附加文件扩展名如果你想保留它(拆分file.name在'。'和得到最后一个段)

我们不知道开发人员想要哪个版本的独特文件(见下文),因为有很多用例,所以我们决定离开选择给开发者。

  images / uuid / image.png //选项1:在UUID文件夹下的干净名称
image / uuid.png //选项2:唯一的名称,相同的扩展名
images / uuid //选项3:无扩展名

在我看来,这似乎是一个合理的解释,所以我会在内部提交一个bug来记录它:)


With the new Firebase API you can upload files into cloud storage from client code. The examples assume the file name is known or static during upload:

// 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');

or

// 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);

With users uploading their own files, name conflicts are going to be an issue. How can you have Firebase create a filename instead of defining it yourself? Is there something like the push() feature in the database for creating unique storage references?

解决方案

Firebase Storage Product Manager here:

TL;DR: Use a UUID generator (in Android (UUID) and iOS (NSUUID) they are built in, in JS you can use something like this: Create GUID / UUID in JavaScript?), then append the file extension if you want to preserve it (split the file.name on '.' and get the last segment)

We didn't know which version of unique files developers would want (see below), since there are many, many use cases for this, so we decided to leave the choice up to developers.

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

It seems to me like this would be a reasonable thing to explain in our documentation though, so I'll file a bug internally to document it :)

这篇关于存储具有唯一/随机名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆