Firebase Storage生成的URL不允许访问该文件

时间:2018-12-06 13:20:38

标签: android firebase url firebase-storage

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

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

    }

2 个答案:

答案 0 :(得分:2)

您读取downloadUri变量的方式是:

final Uri downloadUri = taskSnapshot.getUploadSessionUri();

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

遇到问题时,我倾向于使用Firebase文档,该文档具有方便的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()

另请参阅:

答案 1 :(得分:1)

检查您可以更改的Firebase规则进行测试

service firebase.storage {  match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write: if true;    }  }}