Firebase存储URL随新令牌不断变化 [英] Firebase storage URL keeps changing with new token

查看:79
本文介绍了Firebase存储URL随新令牌不断变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firebase数据库和存储来构建社交媒体应用程序.下面是预期的流量.

I'm trying to build a social media application using firebase database and storage. Below is the flow expected.

  1. 用户上传个人资料图片,该图片存储在Firebase存储中的当前用户文件夹中,URL存储在Firebase数据库中,以便快速访问. (效果很好)

  1. User upload a profile picture which is stored on firebase storage in the current user folder and the URL stored in firebase database for quick access. (Works fine)

用户发表想法.这样可以在数据库中保存用户信息,例如帖子消息,用户名和个人资料图像URL. (效果很好).

User post their thoughts. This save users info such as post message, username and profile image URL in databases. (Works fine).

问题

现在的问题是,用户更新了他或她的个人资料图片,这会覆盖Firebase存储中的较旧的个人资料图片(以便管理存储并使所有评论和帖子中的用户图片都相同).在帖子活动中,由于令牌已更改,无法访问较旧的个人资料图片网址.

Problem

The problem now is say a user updates he's or her profile picture, this overrides the older profile image in firebase storage (in order manage storage and to make user image be the same across all comments and post). On the post message activity the older profile image URL can't be accessed cause the token as changed.

我想知道如何解决此问题,以便在所有更新中,firebase存储URL都是静态的(相同).

I will like to know how this can be fixed in such that the firebase storage URL will be static (that is the same) accross all updates.

使用Picasso而非firebase方法获取图像

Using Picasso and not firebase method to get the images

推荐答案

尽管这个问题被问了很长时间,但是我注意到有些人仍然很难解决这个问题,因此我将在下面提供解决方案.

Although this question had been asked for a very long time, but I noticed some people still find it difficult solving this, so I will be providing my solution below.

步骤1 由于更改后存储URL始终是动态的(即posses令牌),所以我要做的就是对所有用户使用 avatar 的图像文件使用通用名称

STEP 1 Since storage URL are always dynamic (i.e the posses token) when changed, what I did was to use generic name for image file stored say avatar for all users

第2步 在存储中为每个用户创建一个目录,如下所示:users/{uid}/avatar.jpg

STEP 2 Create a directory in storage for each user as follows: users/{uid}/avatar.jpg

第3步 使用步骤2中的路径拉出或显示图像(见下文)

STEP 3 Pull or display the image by using the path in step 2 (see below)

ANDROID

StorageReference storageReference = FirebaseStorage.getInstance().getReference("users").child(userId).child("avatar.jpg");
    
Glide.with(context).using(new FirebaseImageLoader()).load(storageReference).diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ch_white_icon).placeholder(R.drawable.ch_white_icon).into(imageView);

WEB

var storage = firebase.storage();
    var pathReference = storage.ref('users/' + userId + '/avatar.jpg');
    pathReference.getDownloadURL().then(function (url) {
        $("#large-avatar").attr('src', url);
    }).catch(function (error) {
        // Handle any errors
    });

有了这个,您就不必再担心动态链接了,每当您将新图像上传到上述路径时,它都会覆盖前一个图像,并且第3步中的代码将为您提供新图像.

With this you don't have to worry with the dynamic link anymore, whenever you upload a new image to the above path, it overrides the previous one and the code in step 3 will give you the new image.

PS:对于Android,如果您不想滑行缓存图像,请不要忘记将DiskCacheStrategy.ALL更改为DiskCacheStrategy.NONE;如果允许缓存,则可以使用时间戳记获取新图像.

PS: For Android Don't forget to change DiskCacheStrategy.ALL to DiskCacheStrategy.NONE if you don't want glide to cache image or you can use timestamp to get new image if cache is allowed.

这篇关于Firebase存储URL随新令牌不断变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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