NodeJS 为什么我无法访问我的Firebase存储映像?

aamkag61  于 12个月前  发布在  Node.js
关注(0)|答案(3)|浏览(120)

我有两个图像文件上传到Firebase存储:

capsule house.jpg通过UI上传(点击上传文件按钮)。
upload_64e8fd.从我的后端服务器(node.js)上传,使用这个:

const bucket = fbAdmin.storage().bucket('gs://assertivesolutions2.appspot.com');
const result = await bucket.upload(files.image.path);

capsulehouse.jps被识别为jpeg,并且在右边空白处提供了到它的链接。如果我点击它,我会在一个新标签中看到我的图像。你可以自己看看:
https://firebasestorage.googleapis.com/v0/b/assertivesolutions2.appspot.com/o/capsule%20house.jpg?alt=media&token=f5e0ccc4-7916-4245-b813-dbdf1838556f
upload_64e8fd.不被识别为任何类型的图像文件,也不提供任何链接。
后端返回的结果是一个巨大的json对象,包含以下字段:

"selfLink": "https://www.googleapis.com/storage/v1/b/assertivesolutions2.appspot.com/o/upload_64e8fd09f787acfe2728ae73158e20ab"
"mediaLink": "https://storage.googleapis.com/download/storage/v1/b/assertivesolutions2.appspot.com/o/upload_64e8fd09f787acfe2728ae73158e20ab?generation=1590547279565389&alt=media"

第一个把我送到一个页面,上面写着:

{
  "error": {
    "code": 401,
    "message": "Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.",
    "errors": [
      {
        "message": "Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.",
        "domain": "global",
        "reason": "required",
        "locationType": "header",
        "location": "Authorization"
      }
    ]
  }
}

第二个给了我类似的东西:

Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.

我的存储桶的规则如下:

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

我允许所有的读写。
那么,为什么它说,我没有访问看到我的图像时,它是通过我的后端服务器上传?
我还想知道为什么它不承认它是一个jpeg时,它是通过我的后端服务器上传,但它通过用户界面上传时,但我想集中在这个问题的访问问题。
谢谢.

ldxq2e6h

ldxq2e6h1#

默认情况下,文件以私有方式上传,除非您更改存储桶设置,如here所述。下面的代码是如何更改文档可见性的示例。

/**
 * {@inheritdoc}
 */
public function setVisibility($path, $visibility)
{
    $object = $this->getObject($path);

    if ($visibility === AdapterInterface::VISIBILITY_PRIVATE) {
        $object->acl()->delete('allUsers');
    } elseif ($visibility === AdapterInterface::VISIBILITY_PUBLIC) {
        $object->acl()->add('allUsers', Acl::ROLE_READER);
    }

    $normalised = $this->normaliseObject($object);
    $normalised['visibility'] = $visibility;

    return $normalised;
}

您可以按照官方文档中的教程检查如何通过控制台设置:Making data public
除此之外,正如@FrankvanPuffelen的评论中所指出的,您将不会为要访问的文件生成URL。你可以找到更多关于here的信息。
让我知道如果信息帮助你!

hujrc8aj

hujrc8aj2#

另一个答案帮助了我!我不知道为什么控制台让我制定那些安全规则,如果他们不适用......
基于nodejs文档(可能还有其他语言),有一种简单的方法可以在上传过程中将文件公开:

const result = await bucket.upload(files.image.path, {public: true});

同样的选项适用于bucket.file().save()和类似的API。

w6lpcovy

w6lpcovy3#

您需要允许公共读访问

public String uploadImage(String name, String image) {
    final byte[] bytes = Util.convertBase64ToByteArray(image);
    final String imageExtension = Util.getImageExtensionFromBase64(image);
    final String img = name+"."+imageExtension;

    Storage storage = StorageOptions.getDefaultInstance().getService();

    final BlobId blobId = BlobId.of(BUCKET_NAME, img);
    final BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
            .setContentType("image/"+imageExtension)
            .setAcl(new ArrayList<>(List.of(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER)))) // allow read access
            .build();
      
    final Blob blob = storage.create(blobInfo, bytes);
    return blob.getMediaLink();
}

相关问题