Firebase SignedURL不等于下载url存储

时间:2017-04-25 21:14:53

标签: android url firebase download firebase-storage

之前我已经问过一个问题来从Firebase Storage获取downloadURL,给出的答案是:

bucket.file(filename).getSignedUrl({
  action: 'read',
  expires: '03-17-2025'
}, function(err, url) {
  if (err) {
    console.error(err);
    return;
  }

  // The file is now available to read from this URL.
  request(url, function(err, resp) {
    // resp.statusCode = 200
  });
});

当我使用Glide下载图像或使用ExoPlayer重新生成音频或视频文件时,上一代码给出的代码不起作用。

然而,当文件上传时,我得到的完全不同于Firebase存储的URL(对Glide和ExoPlayer有效):

https://firebasestorage.googleapis.com/v0/b/project-PROYECT_NUMBER.appspot.com/o/Messages%2Fimages%2F-KUj4wvXXl6aj9XXXX%2F1493147111111?alt=media&token=TOKEN

如何才能获得正确的downloadUrl,就像我在服务器端使用Android或iOS SDK一样?

3 个答案:

答案 0 :(得分:2)

在使用Android,iOS或Javascript SDK时,无法像服务器端那样获取下载URL。您必须通过调用getSignedUrl()来生成URL。

您可能想要检查您是否有权阅读给定网址的文件。

默认情况下,您必须进行身份验证。

检查firebase控制台中的存储规则

https://firebase.google.com/docs/storage/security/#authorization

答案 1 :(得分:1)

我希望您通过app上传图片只能na?上传后,您将返回下载链接。我希望这会让你失望。

import com.google.firebase.storage.StorageTask;

 final StorageTask uploadTask = uplodedFileRef.putBytes(data);
    Log.d(TAG, "uploadFilefromPath: " + uplodedFileRef.getPath().toString());
uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle unsuccessful uploads
                    Toast.makeText(acty, "Upload Failed", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    sendResult(false, Uri.EMPTY);
                }
            })
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                    Toast.makeText(acty, "Upload Successful", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    sendResult(true, taskSnapshot.getDownloadUrl());
                }
            })
            .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                    int bytesTransferred = (int) taskSnapshot.getBytesTransferred();
                    int totalBytes = (int) taskSnapshot.getTotalByteCount();
                    int progress = (100 * bytesTransferred) / totalBytes;

                    Log.v(TAG, "Bytes transferred: " + taskSnapshot.getBytesTransferred());
                    Log.v(TAG, "TotalBytes: " + totalBytes);
                    Log.v(TAG, "Upload is: " + progress + "% done");

                    dialog.setContent("Upload in Progress");
                    dialog.getProgressBar().setIndeterminate(false);
                    // double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                    dialog.getProgressBar().setMax(totalBytes / 1024);
                    dialog.setMaxProgress(totalBytes / 1024);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        dialog.getProgressBar().setProgress(bytesTransferred / 1024, true);
                        dialog.setProgress(bytesTransferred / 1024);
                    } else
                        dialog.getProgressBar().setProgress(bytesTransferred / 1024);
                    dialog.setProgress(bytesTransferred / 1024);
                    // dialog.getProgressBar().setProgress((int) progress);
                }
            });

答案 2 :(得分:1)

addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
      @Override
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { //success
            sendResult(true, taskSnapshot.getDownloadUrl());
      }
})

在此taskSnapshot.getDownloadUrl()中,您将收到下载网址,然后将其保存在firebase数据库中,您可以将其作为时间检索