V4 signing is a process you can use to generate signatures for authentication in Cloud Storage XML API requests. This page describes how to use the Google Cloud CLI and Cloud Storage client libraries to create signed URLs, using service account credentials. Signed URLs give time-limited read or write access to a specific Cloud Storage resource. If you want to make your own program for creating signed URLs, read V4 signing with your own program instead.
Before you begin
Before performing the tasks in this page, complete the following steps:
-
Enable the Service Account Credentials API.
-
Create a service account . If you already have a service account that you want to use to sign URLs, you can skip this step.
-
Give the service account sufficient permission such that it could perform the request that the signed URL will make. For example, if your signed URL will allow a user to read object data, the service account must itself have permission to read the object data.
For the tasks described in this guide, ask your administrator to grant one of the following IAM roles to the service account:
-
Storage Object User (
roles/storage.objectUser
): Use this role to create signed URLs for both uploading and downloading objects. This role is also required if the signed URL could overwrite an existing object. -
Storage Object Viewer (
roles/storage.objectViewer
): Use this role if you only want to create signed URLs for downloading objects. -
Storage Object Creator (
roles/storage.objectCreator
): Use this role if you only want to create signed URLs for uploading objects and the uploaded object won't overwrite an existing object in the bucket.
These predefined roles contain the permissions required to make signed URLs that download and upload objects. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
-
storage.objects.get
(not required if you only want to upload objects) -
storage.objects.create
(not required if you only want to download objects) -
storage.objects.delete
(not required if you don't need to overwrite objects when uploading)
You might also be able to get these permissions with custom roles or other predefined roles. To see which roles are associated with which permissions, refer to IAM roles for Cloud Storage .
-
-
Have sufficient permission to sign blobs with the service account. Ask your administrator to grant the Service Account Token Creator role (
roles/iam.serviceAccountTokenCreator
).-
If you use user credentials for authentication, you should be the principal assigned this role, and this role should be granted on the service account that will be used to create the signed URLs.
-
If you use a service account attached to a compute instance for authentication, the service account must be the principal for this role in order to impersonate itself, and this role should be granted on the project that contains the service account.
This predefined role contain the permission required to sign blobs with a service account. To see the exact permission that is required, expand the Required permissionssection:
Required permissions
-
iam.serviceAccounts.signBlob
You might also be able to get this permission with custom roles or other predefined roles. To see which roles are associated with which permissions, refer to IAM roles for Cloud Storage .
For instructions on granting roles on projects, see Manage access to projects .
-
Create a signed URL to download an object
To create a signed URL that can get an object from a bucket, complete the following steps:
Command line
-
Use the
gcloud storage sign-url
command. For example, the following command creates a signed URL that allows users to download an object for 10 minutes:gcloud storage sign-url gs:// BUCKET_NAME / OBJECT_NAME --impersonate-service-account= SERVICE_ACCOUNT_EMAIL --duration=10m
Where:
-
BUCKET_NAME
is the name of the bucket where the object is located. For example,example-bucket
. -
OBJECT_NAME
is the name of the object to download. For example,cat.jpeg
. -
SERVICE_ACCOUNT_EMAIL
is the email address of a service account whose key will do the signing. For example,signed-url-account@my-project.iam.gserviceaccount.com
.
If successful, your response should look like:
--- expiration: '2023-07-14 23:19:35' http_verb: GET resource: gs://example-bucket/cat.jpeg signed_url: https://storage.googleapis.com/example-bucket/cat.jpeg? x-goog-signature=11ae9c61ca84dd0bec319f7d52a38029e5873caa2eeced0568 ef96076258cfc1a925a9683cc907d210036b61af9e06a13bf4a15b15fab3916669b e2f4c9f66ea6be822bec5858af519a6da705415b5768721197be213103fa09b8a18 8a143be77a24351517ff208a2c62cfebb78040daf1f953907080bd98f9462739d11 1355b1d9bcf54705b862f37392c031fde0d52add1a4d3bbb98a22e8b7023f6a1623 2e0a2dd56e524d410624d28663e557fafaf4ba0a04290a1066f894713857b429258 d14f056066c7622baf114c124e645688e19b4df3c4a7925f580693c93fa9c1dae7f dff0edff7259c72f3f0eadc5a9f9f556c83c9c8dc02ee3af8d20ab634bad&x-goog -algorithm=GOOG4-RSA-SHA256&x-goog-credential=signed-url-account%40 my-project.iam.gserviceaccount.com%2F20230714%2Fus%2Fstorage%2Fgoog 4_request&x-goog-date=20230714T221935Z&x-goog-expires=600&x-goog-si gnedheaders=host
This URL can be used by any person to access the associated resource (in this case
cat.jpeg
) for the designated length of time (in this case, 10 minutes). -
Client libraries
C++
For more information, see the Cloud Storage C++ API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
C#
For more information, see the Cloud Storage C# API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
Go
For more information, see the Cloud Storage Go API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
Java
For more information, see the Cloud Storage Java API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
Node.js
For more information, see the Cloud Storage Node.js API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
PHP
For more information, see the Cloud Storage PHP API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
Python
For more information, see the Cloud Storage Python API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
Ruby
For more information, see the Cloud Storage Ruby API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .