Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Cloud Storage for Firebase lưu trữ dữ liệu của bạn trong một vùng chứa Google Cloud Storage – một giải pháp lưu trữ đối tượng ở quy mô exabyte với tính sẵn sàng cao và tính dự phòng toàn cầu. Firebase Admin SDK cho phép bạn truy cập trực tiếp vào các vùng chứa Cloud Storage từ các môi trường có đặc quyền. Sau đó, bạn có thể sử dụng API Google Cloud Storage để thao tác với các đối tượng được lưu trữ trong các vùng chứa.
Admin SDK cũng cho phép bạn tạo URL có thể chia sẻ để người dùng có thể tải các đối tượng xuống trong các vùng chứa của bạn.
Sử dụng bộ chứa mặc định
Bạn có thể chỉ định tên nhóm mặc định khi khởi chạy Admin SDK. Sau đó, bạn có thể truy xuất một thông tin tham chiếu đã xác thực đến nhóm này.
Tên nhóm lưu trữ không được chứa gs:// hoặc bất kỳ tiền tố giao thức nào khác.
Ví dụ: nếu URL của nhóm hiển thị trong bảng điều khiển Firebase là gs://PROJECT_ID.firebasestorage.app, hãy truyền chuỗi PROJECT_ID.firebasestorage.app đến Admin SDK.
Node.js
const{initializeApp,cert}=require('firebase-admin/app');const{getStorage}=require('firebase-admin/storage');constserviceAccount=require('./path/to/serviceAccountKey.json');initializeApp({credential:cert(serviceAccount),storageBucket:'<BUCKET_NAME>.appspot.com'});constbucket=getStorage().bucket();// 'bucket' is an object defined in the @google-cloud/storage library.// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/latest/storage/bucket// for more details.
Java
FileInputStreamserviceAccount=newFileInputStream("path/to/serviceAccountKey.json");FirebaseOptionsoptions=FirebaseOptions.builder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).setStorageBucket("<BUCKET_NAME>.appspot.com").build();FirebaseApp.initializeApp(options);Bucketbucket=StorageClient.getInstance().bucket();// 'bucket' is an object defined in the google-cloud-storage Java library.// See https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket// for more details.
Python
importfirebase_adminfromfirebase_adminimportcredentialsfromfirebase_adminimportstoragecred=credentials.Certificate('path/to/serviceAccountKey.json')firebase_admin.initialize_app(cred,{'storageBucket':'PROJECT_ID.firebasestorage.app'})bucket=storage.bucket()# 'bucket' is an object defined in the google-cloud-storage Python library.# See https://googlecloudplatform.github.io/google-cloud-python/latest/storage/buckets.html# for more details.
Go
import("context""log"firebase"firebase.google.com/go/v4""firebase.google.com/go/v4/auth""google.golang.org/api/option")config:=&firebase.Config{StorageBucket:"<BUCKET_NAME>.appspot.com",}opt:=option.WithCredentialsFile("path/to/serviceAccountKey.json")app,err:=firebase.NewApp(context.Background(),config,opt)iferr!=nil{log.Fatalln(err)}client,err:=app.Storage(context.Background())iferr!=nil{log.Fatalln(err)}bucket,err:=client.DefaultBucket()iferr!=nil{log.Fatalln(err)}// 'bucket' is an object defined in the cloud.google.com/go/storage package.// See https://godoc.org/cloud.google.com/go/storage#BucketHandle// for more details.
Bạn có thể sử dụng các thông tin tham chiếu đến vùng chứa mà Admin SDK trả về cùng với Google Cloud Storage thư viện ứng dụng chính thức để tải lên, tải xuống và sửa đổi nội dung trong các vùng chứa được liên kết với dự án Firebase của bạn. Xin lưu ý rằng bạn không cần xác thực các thư viện Google Cloud Storage khi sử dụng Firebase Admin SDK. Các tham chiếu đến vùng chứa do Admin SDK trả về đã được xác thực bằng thông tin đăng nhập dùng để khởi chạy ứng dụng Firebase của bạn.
Sử dụng nhóm tuỳ chỉnh
Nếu muốn sử dụng một vùng chứa Cloud Storage khác với vùng chứa mặc định được mô tả trước đó trong hướng dẫn này hoặc sử dụng nhiều vùng chứa Cloud Storage trong một ứng dụng, bạn có thể truy xuất một tham chiếu đến vùng chứa tuỳ chỉnh:
Nếu đang tạo một ứng dụng phức tạp hơn tương tác với nhiều ứng dụng Firebase, bạn có thể truy cập vào các vùng chứa Cloud Storage được liên kết với một ứng dụng Firebase cụ thể như sau:
Bạn có thể sử dụng Admin SDK để tạo URL tải xuống không hết hạn cho các tệp được lưu trữ trong các vùng chứa của bạn. Bất kỳ ai có URL này đều có thể truy cập vĩnh viễn vào tệp.
Firebase Admin SDK phụ thuộc vào Google Cloud Storagethư viện ứng dụng để cung cấp quyền truy cập Cloud Storage. Các tham chiếu đến bộ chứa do Admin SDK trả về là các đối tượng được xác định trong những thư viện này. Tham khảo tài liệu và tài liệu tham khảo API của thư viện ứng dụng Google Cloud Storage để tìm hiểu cách sử dụng các tài liệu tham khảo về nhóm được trả về trong các trường hợp sử dụng như tải lên và tải xuống tệp.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 UTC."],[],[],null,["\u003cbr /\u003e\n\nCloud Storage for Firebase stores your data in a\n[Google Cloud Storage](//cloud.google.com/storage) bucket --- an\nexabyte scale object storage solution with high availability and global\nredundancy. The Firebase Admin SDK allows you to directly access your\nCloud Storage buckets from privileged environments. Then you can use\n[Google Cloud Storage APIs](//cloud.google.com/storage/docs/reference/libraries)\nto manipulate the objects stored in the buckets.\n\nThe Admin SDK also lets you to create shareable URLs so users can\ndownload objects in your buckets.\n| **Important** : The following changes to pricing plan requirements are happening for Cloud Storage for Firebase. Learn more in the [FAQs](/docs/storage/faqs-storage-changes-announced-sept-2024).\n|\n| - **Starting\n| October 30, 2024** , your Firebase project must be on the [pay-as-you-go Blaze pricing plan](/pricing) to provision a new Cloud Storage for Firebase default bucket. The bucket can optionally use the [\"Always Free\" tier](https://cloud.google.com/storage/pricing#cloud-storage-always-free) for Google Cloud Storage.\n| - **Starting\n| October 1, 2025** , your Firebase project must be on the [pay-as-you-go Blaze pricing plan](/pricing) to maintain access to your default bucket and all other Cloud Storage resources. Any `*.appspot.com` default bucket will maintain its current no-cost level of usage even on the Blaze pricing plan.\n\nUse a default bucket\n\nYou can specify a default bucket name when initializing the Admin SDK. Then you\ncan retrieve an authenticated reference to this bucket.\n\nThe bucket name must *not* contain `gs://` or any other protocol prefixes.\nFor example, if the bucket URL displayed in the\n[Firebase console](//console.firebase.google.com/project/_/storage)\nis `gs://`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`.firebasestorage.app`, pass the string\n\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`.firebasestorage.app` to the Admin SDK. \n\nNode.js \n\n const { initializeApp, cert } = require('firebase-admin/app');\n const { getStorage } = require('firebase-admin/storage');\n\n const serviceAccount = require('./path/to/serviceAccountKey.json');\n\n initializeApp({\n credential: cert(serviceAccount),\n storageBucket: '\u003cBUCKET_NAME\u003e.appspot.com'\n });\n\n const bucket = getStorage().bucket();\n\n // 'bucket' is an object defined in the @google-cloud/storage library.\n // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/latest/storage/bucket\n // for more details.\n\nJava \n\n FileInputStream serviceAccount = new FileInputStream(\"path/to/serviceAccountKey.json\");\n\n FirebaseOptions options = FirebaseOptions.builder()\n .setCredentials(GoogleCredentials.fromStream(serviceAccount))\n .setStorageBucket(\"\u003cBUCKET_NAME\u003e.appspot.com\")\n .build();\n FirebaseApp.initializeApp(options);\n\n Bucket bucket = StorageClient.getInstance().bucket();\n\n // 'bucket' is an object defined in the google-cloud-storage Java library.\n // See https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket\n // for more details.\n\nPython \n\n import firebase_admin\n from firebase_admin import credentials\n from firebase_admin import storage\n\n cred = credentials.Certificate('path/to/serviceAccountKey.json')\n firebase_admin.initialize_app(cred, {\n 'storageBucket': '\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e.firebasestorage.app'\n })\n\n bucket = storage.bucket()\n\n # 'bucket' is an object defined in the google-cloud-storage Python library.\n # See https://googlecloudplatform.github.io/google-cloud-python/latest/storage/buckets.html\n # for more details.\n\nGo \n\n import (\n \t\"context\"\n \t\"log\"\n\n \tfirebase \"firebase.google.com/go/v4\"\n \t\"firebase.google.com/go/v4/auth\"\n \t\"google.golang.org/api/option\"\n )\n\n config := &firebase.Config{\n \tStorageBucket: \"\u003cBUCKET_NAME\u003e.appspot.com\",\n }\n opt := option.WithCredentialsFile(\"path/to/serviceAccountKey.json\")\n app, err := firebase.NewApp(context.Background(), config, opt)\n if err != nil {\n \tlog.Fatalln(err)\n }\n\n client, err := app.Storage(context.Background())\n if err != nil {\n \tlog.Fatalln(err)\n }\n\n bucket, err := client.DefaultBucket()\n if err != nil {\n \tlog.Fatalln(err)\n }\n // 'bucket' is an object defined in the cloud.google.com/go/storage package.\n // See https://godoc.org/cloud.google.com/go/storage#BucketHandle\n // for more details. \n https://github.com/firebase/firebase-admin-go/blob/26dec0b7589ef7641eefd6681981024079b8524c/snippets/storage.go#L31-L51\n\nYou can use the bucket references returned by the Admin SDK in conjunction with\nthe official\n[Google Cloud Storage client libraries](//cloud.google.com/storage/docs/reference/libraries)\nto upload, download, and modify content in the buckets associated with your\nFirebase projects. Note that you do not have to authenticate\nGoogle Cloud Storage libraries when using the Firebase Admin SDK. The bucket\nreferences returned by the Admin SDK are already authenticated with the\ncredentials used to initialize your Firebase app.\n\nUse custom buckets\n\nIf you want to use a Cloud Storage bucket other than the default bucket\ndescribed earlier in this guide, or use multiple Cloud Storage buckets in\na single app, you can retrieve a reference to a custom bucket: \n\nNode.js \n\n const bucket = getStorage().bucket('my-custom-bucket');\n\nJava \n\n Bucket bucket = StorageClient.getInstance().bucket(\"my-custom-bucket\");\n\nPython \n\n bucket = storage.bucket('my-custom-bucket')\n\nGo \n\n bucket, err := client.Bucket(\"my-custom-bucket\") \n https://github.com/firebase/firebase-admin-go/blob/26dec0b7589ef7641eefd6681981024079b8524c/snippets/storage.go#L64-L64\n\nUse a custom Firebase app\n\nIf you are building a more complicated application that interacts with\n[multiple Firebase apps](/docs/admin/setup#initialize-multiple-apps), you can\naccess the Cloud Storage buckets associated with a specific Firebase app\nas follows: \n\nNode.js \n\n const bucket = getStorage(customApp).bucket();\n\nJava \n\n Bucket bucket = StorageClient.getInstance(customApp).bucket();\n\nPython \n\n bucket = storage.bucket(app=custom_app)\n\nGo \n\n otherClient, err := otherApp.Storage(context.Background())\n bucket, err := otherClient.Bucket(\"other-app-bucket\")\n\nGet a shareable download URL\n\nYou can use the Admin SDK to generate a non-expiring download URL for\nfiles stored in your buckets. Anyone with this URL can permanently\naccess the file. \n\nNode.js \n\n const { getStorage, getDownloadURL } = require('firebase-admin/storage');\n\n const fileRef = getStorage().bucket('my-bucket').file('my-file');\n const downloadURL= await getDownloadURL(fileRef);\n\nGoogle Cloud Storage client libraries\n\nThe Firebase Admin SDKs depend on the\n[Google Cloud Storage client libraries](//cloud.google.com/storage/docs/reference/libraries)\nto provide Cloud Storage access. The bucket references returned by the\nAdmin SDK are objects defined in these libraries. Refer to the documentation and\nAPI references of the Google Cloud Storage client libraries to learn how to\nuse the returned bucket references in use cases like file\n[upload](//cloud.google.com/storage/docs/uploading-objects) and\n[download](//cloud.google.com/storage/docs/downloading-objects)."]]