【问题标题】:Invalid Bucket Name when uploading to Firebase storage?上传到 Firebase 存储时存储桶名称无效?
【发布时间】:2021-08-24 10:11:08
【问题描述】:

我正在尝试将媒体(图片)上传到 Firebase 存储。存储桶路径是

gs://sample-ccc80.appspot.com/SAMPLE/IMAGES

Firebase 规则:

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write;
}
}
}

我正在关注这个blog 作为教程。这是我得到的回复:

"message" : "Invalid bucket name: 'sample-ccc80.appspot.com/SAMPLE/IMAGES'"

#代码

 public static String uploadFile(File file, String fileName, String bucketName) throws IOException {
    BlobId blobId = BlobId.of(bucketName, fileName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("media").build();
    String absolutePath = "/service-account.json";
    Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(absolutePath));
    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
    storage.create(blobInfo, Files.readAllBytes(file.toPath()));
    String DOWNLOAD_URL  = "https://firebasestorage.googleapis.com/v0/b/" + bucketName + "/o/%s?alt=media";
    return String.format(DOWNLOAD_URL, URLEncoder.encode(fileName, "UTF-8"));
}

我在这里做错了什么?

【问题讨论】:

    标签: java firebase spring-boot firebase-storage


    【解决方案1】:

    Cloud Storage 存储分区名称不能包含/(斜杠),请参阅doc on "Bucket naming guidelines"

    我猜你想上传到你的存储桶的SAMPLE/IMAGES“文件夹”,但谷歌云存储没有真正的“文件夹”,正如anwser中所解释的那样。

    您需要使用fileName 变量来传递这个“文件夹”,例如SAMPLE/IMAGES/MyHolidaysNicePicture.png。您的存储桶名称应为gs://sample-ccc80.appspot.com

    【讨论】:

    • 感谢您的回复。将存储桶名称更改为gs://sample-ccc80.appspot.com 后。我仍然收到相同的错误消息
    猜你喜欢
    • 2019-04-27
    • 1970-01-01
    • 2018-10-25
    • 2016-12-27
    • 2021-05-20
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多