Firebase Storage生成的URL不允许访问该文件 [英] URL generated by Firebase Storage does not allow access to the file

查看:104
本文介绍了Firebase Storage生成的URL不允许访问该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问图像的网址时,我无法在Firebase Storage中检索图像,并显示以下消息:"X-Goog-Upload-Command标头,它不是有效的X-Goog文件" .我无法通过下载URL访问存储在存储中的文件.我寻求帮助,下面的代码是我将文件上传到存储和数据库的方式!

I can't retrieve my images in Firebase Storage, when I try to access the Url of the image, shows this message: "the X-Goog-Upload-Command header, which is not a valid X-Goog file". I can not access the files stored in Storage through the download URL. I ask for help, and the code bellow is how I'm uploading my files to the Storage and Database!

    if (pmcUri != null){

        // show the progress Dialog
        pmcProgress = new ProgressDialog(this);
        pmcProgress.setTitle("Creating your Feed");
        pmcProgress.setMessage("...");
        pmcProgress.show();

        final StorageReference mChildStorage = pmcStorage.child("feedPhotos")
                .child(pmcUri.getLastPathSegment());
        mChildStorage.putFile(pmcUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                final Uri downloadUri = taskSnapshot.getUploadSessionUri();

                FirebaseDatabase database = FirebaseDatabase.getInstance();


                mChildStorage.getDownloadUrl();
                // save infos in Database with user ID as a Reference
                final DatabaseReference myRef = database.getReference("feeds").child(uid.getUid());
                myRef.child("feed").setValue(setName);
                assert downloadUri != null;
                myRef.child("feedimg").setValue(downloadUri.toString());
                myRef.child("feedkey").setValue(uid.getUid());


                pmcProgress.dismiss();

                Intent intent = new Intent(create_feed.this, glime.class);
                startActivity(intent);
            }
        });

    }

推荐答案

读取 downloadUri 变量的方式是:

final Uri downloadUri = taskSnapshot.getUploadSessionUri();

调用 getUploadSessionUri()为您提供 upload 会话的URI,而不是文件上传完成后的下载URL.下载URL从任务快照中不再可用,而是需要对服务器的其他异步调用.

Calling getUploadSessionUri() gives you a URI for th upload session, not the download URL for the file once it's finished uploading. The download URL is no longer available from the task snapshot, but instead requires an additional asynchronous call to the server.

遇到问题时,我倾向于使用Firebase文档,该文档很方便上传完成后获取下载URL的示例:

When I have problems, I tend to fall back to the Firebase documentation, which has this handy example of getting the download URL after an upload has completed:

final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
        } else {
            // Handle failures
            // ...
        }
    }
});

因此,要在上传完成后获取下载URL,请启动一个新任务,该任务返回 ref.getDownloadUrl().

So to get the download URL after the upload has completed, you start a new task that returns ref.getDownloadUrl().

另请参阅:

  • How to use getdownloadurl in recent versions?, which shows the simplest possible snippet to get a download URL, and itself links to even more questions about this.
  • Firebase Storage getDownloadUrl() method can't be resolved, which is the error you probably got leading you to use getUploadSessionUri().
  • getDownloadURL isn't inputting the link i need

这篇关于Firebase Storage生成的URL不允许访问该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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