This document shows how to add users to a Cloud FTP server. Users such as external partners and internal stakeholders can use the server to securely transfer files to and from Cloud Storage.
At a high level, these are the steps to add a user to an SFTP server:
- Configure permissions for the user.
- Create an SFTP user and map their directories to Cloud Storage buckets.
For steps to create a server, see Create an external SFTP server and Create an internal SFTP server.
Considerations
You can grant a user access to a maximum of 10 buckets.
A user can have a maximum of 10 public keys.
Before you begin
Create one or more Cloud Storage buckets to store the data that the user will work with, if the buckets don't already exist.
Get the user's public key from each SSH key pair that they will use to connect to the server.
If the user doesn't have an SSH key pair, use the following steps to generate one.
Generate an SSH key pair
Which key format do you need?
The key format that you need depends on which client you use to connect to the SFTP server. For more information, see Supported SFTP clients.
PEM format
-
To create an OpenSSH key pair (PEM format), use the
ssh-keygenutility.On the client machine that will connect to the SFTP server, run the following command:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/KEY_PAIR_NAME
Replace
KEY_PAIR_NAMEwith a name for the key pair, such assftp_user_key. -
Extract the public key:
cat ~/.ssh/KEY_PAIR_NAME.pub
PPK format
-
To create a PuTTY key pair (PPK format), use the
PuTTYgentool.On the client machine that will connect to the SFTP server, run the following command:
puttygen -t rsa -b 4096 -o KEY_PAIR_NAME.ppk
Replace
KEY_PAIR_NAMEwith a name for the key pair, such assftp_user_key. -
Extract the public key:
puttygen -L KEY_PAIR_NAME.ppk
-
Required roles
To get the permission that
you need to add SFTP users,
ask your administrator to grant you the
FTP Admin (roles/ftp.admin) IAM role on your project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the
ftp.users.create
permission,
which is required to
add SFTP users.
You might also be able to get this permission with custom roles or other predefined roles.
Configure permissions for a user
-
Install the Google Cloud CLI, and then sign in to the gcloud CLI with your federated identity. After signing in, initialize the Google Cloud CLI by running the following command:
gcloud init Set the project:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith the ID of the project that contains the SFTP server.Create a service account for the user, if a service account doesn't already exist. The service account accesses Cloud Storage resources on behalf of the user.
gcloud iam service-accounts create USERNAME-sa \ --description="USERNAME SFTP Service Account" \ --display-name="USERNAME SFTP Service Account"
Replace
USERNAMEwith a unique username for the SFTP user. The username must start with a lowercase letter and can include lowercase letters, numbers, or hyphens. Maximum length is 32 characters.On success, a message like
Created service account [example-userid-sa]is displayed.Grant IAM roles to the administrator who creates the SFTP user:
Grant yourself the Service Account User (
roles/iam.serviceAccountUser) role:gcloud iam service-accounts add-iam-policy-binding USERNAME-sa@PROJECT_ID. \ --member="user:ADMINISTRATOR_EMAIL" \ --role="roles/iam.serviceAccountUser"
Replace
ADMINISTRATOR_EMAILwith the email address of the principal who is creating the SFTP user. If you're creating the user (as opposed to an application), then this value is the email address that you use to access Google Cloud.On success, a message like the following is displayed:
Updated IAM policy for serviceAccount [example-userid-sa@example-project.]. bindings: - members: - user:admin@example.com role: roles/iam.serviceAccountUser etag: BwZJk7OiSzw= version: 1Grant yourself the Storage Bucket Viewer (
roles/storage.bucketViewer) role on the bucket:gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \ --member="user:ADMINISTRATOR_EMAIL" \ --role="roles/storage.bucketViewer"
Replace
BUCKET_NAMEwith the name of the bucket.Repeat this step for each bucket that the user needs to access.
Grant yourself the Storage Object Viewer (
roles/storage.objectViewer) role on the bucket:gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \ --member="user:ADMINISTRATOR_EMAIL" \ --role="roles/storage.objectViewer"
Repeat this step for each bucket that the user needs to access.
Grant the user's service account the IAM role that's necessary to access the bucket:
gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \ --member="serviceAccount:USERNAME-sa@PROJECT_ID." \ --role="ROLE"
Replace
ROLEwith one of the following IAM roles:- For read-only access, use the
roles/storage.objectViewerrole. - For read and write access, use the
roles/storage.objectAdminrole.
Repeat this step for each bucket that the user needs to access.
- For read-only access, use the
Authorize the Cloud FTP Service Agent to generate tokens for the user's service account:
Get the email address of the server's service agent. For steps, see Get details about a server.
Authorize the Cloud FTP Service Agent:
gcloud iam service-accounts add-iam-policy-binding USERNAME-sa@PROJECT_ID. \ --member="serviceAccount:SERVICE_AGENT_EMAIL" \ --role="roles/iam.serviceAccountTokenCreator"
Replace
SERVICE_AGENT_EMAILwith the service agent's email address.
Next, create a user.
Create an SFTP server user
After you configure permissions for a user, you create the user and map their directories to one or more Cloud Storage buckets.
gcloud
To create a user for an SFTP server, run the
gcloud alpha storage ftp users createcommand.Before using any of the command data below, make the following replacements:
- CREDENTIAL_NAME: a unique name to identify the user credentials.
- SSH_PUBLIC_KEY: the body of the user's SSH public key, in OpenSSH format. For
example,
ssh-rsa AAAAB3NzaC1ycRexample.... - USERNAME: the SFTP user's username.
- SERVICE_ACCOUNT: the user's service account. For example,
username-sa@example-project.. - LOCATION_ID: the location of the server, such
as
us-west1. - SERVER_ID: the server ID.
- BUCKET_NAME: the name of a bucket that the SFTP user is granted access to, such as
example-bucket. Omit thegs://. - (Optional) BUCKET_PREFIX: the path of a folder within the bucket to set as the
root directory for this directory mapping.
If you omit the
bucket_prefixproperty, then Cloud FTP uses the bucket root. -
DIRECTORY: the logical landing directory path that is presented to the SFTP user. For example,
/home/uploads.If you map multiple buckets or folders, then provide a unique directory path for each mapping.
Nested logical directories aren't supported. If you provide multiple mappings for a directory, then provide the directories in a flat structure instead of a nested structure. For example, use
/dir1and/dir2instead of/dir1and/dir1/dir2. - SFTP_PERMISSION: the access level for the directory. For read-only access, set this
value to
READ_ONLY. For read and write access, set this value toREAD_WRITE.
Note the following:
- To grant a user access to multiple buckets, provide the
--storage-directory-mappingflag multiple times, with a directory mapping for each bucket. - To configure multiple public keys for a user, provide multiple credentials in the
credentials.jsonfile.
Save the following content in a file called
credentials.json:[ { "credentialName": "CREDENTIAL_NAME", "credentialType": "PUBLIC_KEY", "sshPublicKeyBody": "SSH_PUBLIC_KEY" } ]
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud alpha storage ftp users create USERNAME \ --customer-service-account=SERVICE_ACCOUNT --location=LOCATION_ID \ --server=SERVER_ID \ --storage-directory-mapping=bucket=BUCKET_NAME,bucket_prefix=BUCKET_PREFIX,directory=DIRECTORY,permission=SFTP_PERMISSION \ --user-credentials-from-file=credentials.json
Windows (PowerShell)
gcloud alpha storage ftp users create USERNAME ` --customer-service-account=SERVICE_ACCOUNT --location=LOCATION_ID ` --server=SERVER_ID ` --storage-directory-mapping=bucket=BUCKET_NAME,bucket_prefix=BUCKET_PREFIX,directory=DIRECTORY,permission=SFTP_PERMISSION ` --user-credentials-from-file=credentials.json
It takes a few seconds for the user to be created.Windows (cmd.exe)
gcloud alpha storage ftp users create USERNAME ^ --customer-service-account=SERVICE_ACCOUNT --location=LOCATION_ID ^ --server=SERVER_ID ^ --storage-directory-mapping=bucket=BUCKET_NAME,bucket_prefix=BUCKET_PREFIX,directory=DIRECTORY,permission=SFTP_PERMISSION ^ --user-credentials-from-file=credentials.json
Give the user the following information, which they need to connect to the server:
The user's SFTP username.
The server's access configuration, which depends on the server type:
For an external server, the server's IP address.
For an internal server, the URI of the server's service attachment.
For steps to get the server's access configuration, see Get details about a server.
(Optional) The key fingerprint of the server.
REST
To create a user for an SFTP server, use the
servers.users.createmethod.Before using any of the request data, make the following replacements:
- PROJECT_ID: the Google Cloud project ID of the server.
- LOCATION_ID: the location of the server, such
as
us-west1. - SERVER_ID: the server ID.
- USERNAME: the SFTP user's username.
- BUCKET_NAME: the name of a bucket that the SFTP user is granted access to, such as
example-bucket. Omit thegs://. - (Optional) BUCKET_PREFIX: the path of a folder within the bucket to set as the root directory for this directory mapping. If you omit this value, then Cloud FTP uses the bucket root.
-
DIRECTORY: the logical landing directory path that is presented to the SFTP user. For example,
/home/uploads. If you omit this value, then Cloud FTP sets the landing directory to/.If you map multiple buckets or folders, then provide a unique directory path for each mapping.
Nested logical directories aren't supported. If you provide multiple mappings for a directory, then provide the directories in a flat structure instead of a nested structure. For example, use
/dir1and/dir2instead of/dir1and/dir1/dir2. - SFTP_PERMISSION: the access level for the directory. For read-only access, set this
value to
READ_ONLY. For read and write access, set this value toREAD_WRITE. - SERVICE_ACCOUNT: the user's service account. For example,
username-sa@example-project.. - CREDENTIAL_NAME: a unique name to identify the user credentials.
- SSH_PUBLIC_KEY: the body of the user's SSH public key, in OpenSSH format. For
example,
ssh-rsa AAAAB3NzaC1ycRexample....
Note the following:
- To grant a user access to multiple buckets, provide multiple bucket mappings in the
storageDirectoryMappingslist. - To configure multiple public keys for a user, provide multiple credentials in the
userCredentialslist.
HTTP method and URL:
POST https://ftp.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/servers/SERVER_ID/users?userId=USERNAME
Request JSON body:
{ "storageDirectoryMappings": [ { "bucket": "BUCKET_NAME", "bucketPrefix": "BUCKET_PREFIX", "directory": "DIRECTORY", "permission": "SFTP_PERMISSION" } ], "customerServiceAccount": "SERVICE_ACCOUNT", "userCredentials": [ { "credentialName": "CREDENTIAL_NAME", "credentialType": "PUBLIC_KEY", "sshPublicKeyBody": "SSH_PUBLIC_KEY" } ] }To send your request, expand one of these options:
The response identifies a long-running operation. It takes a few seconds for the user to be created.Give the user the following information, which they need to connect to the server:
The user's SFTP username.
The server's access configuration, which depends on the server type:
For an external server, the server's IP address.
For an internal server, the URI of the server's service attachment.
For steps to get the server's access configuration, see Get details about a server.
(Optional) The key fingerprint of the server.
What's next
- Connect to an external SFTP server.
- Connect to an internal SFTP server.
- Transfer data by using SFTP commands.
- Learn about data protection, backup, and recovery options for Cloud Storage buckets.