This document shows how to create an external SFTP server by using
Cloud FTP. An external SFTP server can be accessed over the internet.
Partners who are outside of your network can use the server to securely transfer
files to and from Cloud Storage.

You can restrict access to an external SFTP server based on the IP addresses of
your users.

For steps to create an internal server, which can be accessed only within your
Virtual Private Cloud (VPC) network, see
[Create an internal SFTP server](https://docs.cloud.google.com/cloud-ftp/create-internal-sftp-server.md.txt).
You can't change whether a server is external or internal after the server has
been created.

## Before you begin

### Console


1. If you want to restrict access to the server based on your users' IP addresses, collect a list of CIDR blocks that are allowed to access the server.

<br />

### gcloud


1. If you want to restrict access to the server based on your users' IP addresses, collect a list of CIDR blocks that are allowed to access the server.

<br />

### Required roles


To get the permissions that
you need to create an SFTP server,

ask your administrator to grant you the
[Cloud FTP Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/ftp.md.txt#ftp.admin) (`roles/ftp.admin`) IAM role on your project.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


This predefined role contains

the permissions required to create an SFTP server. To see the exact permissions that are
required, expand the **Required permissions** section:


#### Required permissions

The following permissions are required to create an SFTP server:

- `ftp.servers.create`
- If you use the Google Cloud console to do this task:
  - `ftp.servers.list`
  - `ftp.locations.list`


You might also be able to get
these permissions
with [custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt) or
other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#predefined).

## Create an external server

### Console

1. In the Google Cloud console, go to the **Cloud FTP** page.

   [Go to Cloud FTP](https://console.cloud.google.com/transfer/ftp-servers)
2. Click **Create server**.

3. On the **Server configuration** tab, enter basic details for the server:

   1. In the **Server ID** field, enter a unique ID for the server.

   2. In the **Region** list, select the
      [Google Cloud location](https://docs.cloud.google.com/cloud-ftp/locations.md.txt) for
      the server.

      For the fastest data transfer speeds, create the server in the same
      location as the Cloud Storage bucket that stores your data.
      If the bucket's location isn't supported by Cloud FTP,
      then select the nearest available location.
   3. (Optional) Enter a display name for the server.

   4. Click **Continue**.

4. On the **Networking and security** tab, configure connectivity for the
   server:

   1. Select **External (Public IP)**.

   2. In the **Allowed IP ranges** field, provide a CIDR block that is
      allowed to access the server, and then press <kbd>Enter</kbd>. For
      example, `192.0.2.0/24`. You must provide at least one value, and can
      provide a maximum of 500 CIDR blocks.
      To allow connections from all IP addresses, use `0.0.0.0/0`.

5. Click **Create**.

   It takes about 10 minutes for the server to be
   created.

### gcloud

To create an external server, run the
[`gcloud alpha storage ftp servers create` command](https://docs.cloud.google.com/sdk/gcloud/reference/alpha/storage/ftp/servers/create.md.txt)
and provide the `--access-type=EXTERNAL` flag.


Before using any of the command data below,
make the following replacements:

- <var translate="no">SERVER_ID</var>: a unique ID for the server. Must start with a lowercase letter, and end with a lowercase letter or number. Can contain lowercase letters, numbers, and hyphens. Can't begin with the prefix `goog` or contain the string `google`, including close misspellings such as `g00gle`. Maximum length is 30 characters.
- <var translate="no">LOCATION_ID</var>: the [Google Cloud location](https://docs.cloud.google.com/cloud-ftp/locations.md.txt) of
  the server, such as `us-west1`.

  For the fastest data transfer speeds, create the
  server in the same location as the Cloud Storage bucket that stores your data. If the
  bucket's location isn't supported by Cloud FTP, then select the nearest available
  location.
- <var translate="no">CIDR_BLOCK</var>: a CIDR block that is allowed to access the server. For example, `192.0.2.0/24`. You must provide at least one value, and can provide a maximum of 500 CIDR blocks in a comma-separated list. To allow connections from all IP addresses, use `0.0.0.0/0`.


Execute the

following

command:

#### Linux, macOS, or Cloud Shell

> [!NOTE]
> **Note:** Ensure you have initialized the Google Cloud CLI with authentication and a project by running either [gcloud init](https://docs.cloud.google.com/sdk/gcloud/reference/init); or [gcloud auth login](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) and [gcloud config set project](https://docs.cloud.google.com/sdk/gcloud/reference/config/set.md.txt).

```bash
gcloud alpha storage ftp servers create SERVER_ID --access-type=EXTERNAL \
    --location=LOCATION_ID --allowed-cidr-blocks=CIDR_BLOCK
```

#### Windows (PowerShell)

> [!NOTE]
> **Note:** Ensure you have initialized the Google Cloud CLI with authentication and a project by running either [gcloud init](https://docs.cloud.google.com/sdk/gcloud/reference/init); or [gcloud auth login](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) and [gcloud config set project](https://docs.cloud.google.com/sdk/gcloud/reference/config/set.md.txt).

```bash
gcloud alpha storage ftp servers create SERVER_ID --access-type=EXTERNAL `
    --location=LOCATION_ID --allowed-cidr-blocks=CIDR_BLOCK
```

#### Windows (cmd.exe)

> [!NOTE]
> **Note:** Ensure you have initialized the Google Cloud CLI with authentication and a project by running either [gcloud init](https://docs.cloud.google.com/sdk/gcloud/reference/init); or [gcloud auth login](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) and [gcloud config set project](https://docs.cloud.google.com/sdk/gcloud/reference/config/set.md.txt).

```bash
gcloud alpha storage ftp servers create SERVER_ID --access-type=EXTERNAL ^
    --location=LOCATION_ID --allowed-cidr-blocks=CIDR_BLOCK
```

<br />

It takes about 10 minutes for the server to be
created.

### REST

To create an external server, use the
[`servers.create` method](https://docs.cloud.google.com/cloud-ftp/reference/rest/v1/projects.locations.servers/create.md.txt)
and provide an `externalConfig` access configuration.


Before using any of the request data,
make the following replacements:

- <var translate="no">PROJECT_ID</var>: the Google Cloud project ID of the server.
- <var translate="no">LOCATION_ID</var>: the [Google Cloud location](https://docs.cloud.google.com/cloud-ftp/locations.md.txt) of
  the server, such as `us-west1`.

  For the fastest data transfer speeds, create the
  server in the same location as the Cloud Storage bucket that stores your data. If the
  bucket's location isn't supported by Cloud FTP, then select the nearest available
  location.
- <var translate="no">SERVER_ID</var>: a unique ID for the server. Must start with a lowercase letter, and end with a lowercase letter or number. Can contain lowercase letters, numbers, and hyphens. Can't begin with the prefix `goog` or contain the string `google`, including close misspellings such as `g00gle`. Maximum length is 30 characters.
- <var translate="no">CIDR_BLOCK</var>: a CIDR block that is allowed to access the server. For example, `192.0.2.0/24`. You must provide at least one value, and can provide a maximum of 500 CIDR blocks . To allow connections from all IP addresses, use `0.0.0.0/0`.


HTTP method and URL:

```
POST https://ftp.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/servers?serverId=SERVER_ID
```


Request JSON body:

```
{
  "accessType": "EXTERNAL",
  "externalConfig": {
    "allowedCidrBlocks": [
      "CIDR_BLOCK"
      ]
  }
}
```

To send your request, expand one of these options:

#### curl (Linux, macOS, or Cloud Shell)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`,
and execute the following command:

```
curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d @request.json \
     "https://ftp.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/servers?serverId=SERVER_ID"
```

#### PowerShell (Windows)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`,
and execute the following command:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://ftp.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/servers?serverId=SERVER_ID" | Select-Object -Expand Content
```
The response identifies a [long-running operation](https://docs.cloud.google.com/cloud-ftp/reference/rest/v1/projects.locations.operations.md.txt).

<br />

It takes about 10 minutes for the server to be
created.

### Client libraries


To create an external server, set the access type to external and provide
an external access configuration.

### C#


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP C# API
reference documentation](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Ftp.V1/latest).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```c#
using Google.Cloud.Ftp.V1;
using Google.LongRunning;
using System.Threading.Tasks;

public sealed partial class GeneratedCloudFtpClientSnippets
{
    /// <summary>Snippet for CreateServerAsync</summary>
    /// <remarks>
    /// This snippet has been automatically generated and should be regarded as a code template only.
    /// It will require modifications to work:
    /// - It may require correct/in-range values for request initialization.
    /// - It may require specifying regional endpoints when creating the service client as shown in
    ///   https://cloud.google.com/dotnet/docs/reference/help/client-configuration#endpoint.
    /// </remarks>
    public async Task CreateServerAsync()
    {
        // Create client
        CloudFtpClient cloudFtpClient = await CloudFtpClient.CreateAsync();
        // Initialize request argument(s)
        string parent = "projects/[PROJECT]/locations/[LOCATION]";
        Server server = new Server();
        string serverId = "";
        // Make the request
        Operation<Server, OperationMetadata> response = await cloudFtpClient.CreateServerAsync(parent, server, serverId);

        // Poll until the returned long-running operation is complete
        Operation<Server, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();
        // Retrieve the operation result
        Server result = completedResponse.Result;

        // Or get the name of the operation
        string operationName = response.Name;
        // This name can be stored, then the long-running operation retrieved later by name
        Operation<Server, OperationMetadata> retrievedResponse = await cloudFtpClient.PollOnceCreateServerAsync(operationName);
        // Check if the retrieved long-running operation has completed
        if (retrievedResponse.IsCompleted)
        {
            // If it has completed, then access the result
            Server retrievedResult = retrievedResponse.Result;
        }
    }
}
```

### Go


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP Go API
reference documentation](https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/ftp/latest/apiv1).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```go

//go:build examples

package main

import (
	"context"

	ftp "cloud.google.com/go/ftp/apiv1"
	ftppb "cloud.google.com/go/ftp/apiv1/ftppb"
)

func main() {
	ctx := context.Background()
	// This snippet has been automatically generated and should be regarded as a code template only.
	// It will require modifications to work:
	// - It may require correct/in-range values for request initialization.
	// - It may require specifying regional endpoints when creating the service client as shown in:
	//   https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
	c, err := ftp.NewCloudFtpRESTClient(ctx)
	if err != nil {
		// TODO: Handle error.
	}
	defer c.Close()

	req := &ftppb.CreateServerRequest{
		// TODO: Fill request struct fields.
		// See https://pkg.go.dev/cloud.google.com/go/ftp/apiv1/ftppb#CreateServerRequest.
	}
	op, err := c.CreateServer(ctx, req)
	if err != nil {
		// TODO: Handle error.
	}

	resp, err := op.Wait(ctx)
	if err != nil {
		// TODO: Handle error.
	}
	// TODO: Use resp.
	_ = resp
}
```

### Java


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-ftp/latest/overview).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```java
import com.google.api.core.ApiFuture;
import com.google.cloud.ftp.v1.CloudFtpClient;
import com.google.cloud.ftp.v1.CreateServerRequest;
import com.google.cloud.ftp.v1.LocationName;
import com.google.cloud.ftp.v1.Server;
import com.google.longrunning.Operation;

public class AsyncCreateServer {

  public static void main(String[] args) throws Exception {
    asyncCreateServer();
  }

  public static void asyncCreateServer() throws Exception {
    // This snippet has been automatically generated and should be regarded as a code template only.
    // It will require modifications to work:
    // - It may require correct/in-range values for request initialization.
    // - It may require specifying regional endpoints when creating the service client as shown in
    // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
    try (CloudFtpClient cloudFtpClient = CloudFtpClient.create()) {
      CreateServerRequest request =
          CreateServerRequest.newBuilder()
              .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
              .setServerId("serverId1379103678")
              .setServer(Server.newBuilder().build())
              .setRequestId("requestId693933066")
              .build();
      ApiFuture<Operation> future = cloudFtpClient.createServerCallable().futureCall(request);
      // Do something.
      Operation response = future.get();
    }
  }
}
```

### Node.js


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP Node.js API
reference documentation](https://docs.cloud.google.com/nodejs/docs/reference/ftp/latest).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```javascript
/**
 * This snippet has been automatically generated and should be regarded as a code template only.
 * It will require modifications to work.
 * It may require correct/in-range values for request initialization.
 * TODO(developer): Uncomment these variables before running the sample.
 */
/**
 *  Required. Value for parent.
 */
// const parent = 'abc123'
/**
 *  Required. A unique ID for the server. Must start with a lowercase letter,
 *  and end with a lowercase letter or number. Can contain lowercase letters,
 *  numbers, and hyphens. Maximum length is 30 characters.
 */
// const serverId = 'abc123'
/**
 *  Required. The resource being created
 */
// const server = {}
/**
 *  Optional. An optional request ID to identify requests. Specify a unique
 *  request ID so that if you must retry your request, the server will know to
 *  ignore the request if it has already been completed. The server will
 *  guarantee that for at least 60 minutes since the first request.
 *  For example, consider a situation where you make an initial request and the
 *  request times out. If you make the request again with the same request
 *  ID, the server can check if original operation with the same request ID
 *  was received, and if so, will ignore the second request. This prevents
 *  clients from accidentally creating duplicate commitments.
 *  The request ID must be a valid UUID with the exception that zero UUID is
 *  not supported (00000000-0000-0000-0000-000000000000).
 */
// const requestId = 'abc123'

// Imports the Ftp library
const {CloudFtpClient} = require('@google-cloud/ftp').v1;

// Instantiates a client
const ftpClient = new CloudFtpClient();

async function callCreateServer() {
  // Construct request
  const request = {
    parent,
    serverId,
    server,
  };

  // Run request
  const [operation] = await ftpClient.createServer(request);
  const [response] = await operation.promise();
  console.log(response);
}

callCreateServer();
```

### PHP


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP PHP API
reference documentation](https://docs.cloud.google.com/php/docs/reference/cloud-ftp/latest).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```php
use Google\ApiCore\ApiException;
use Google\ApiCore\OperationResponse;
use Google\Cloud\Ftp\V1\Client\CloudFtpClient;
use Google\Cloud\Ftp\V1\CreateServerRequest;
use Google\Cloud\Ftp\V1\Server;
use Google\Cloud\Ftp\V1\Server\AccessType;
use Google\Rpc\Status;

/**
 * Creates a new Server in a given project and location.
 *
 * @param string $formattedParent  Value for parent. Please see
 *                                 {@see CloudFtpClient::locationName()} for help formatting this field.
 * @param string $serverId         A unique ID for the server. Must start with a lowercase letter,
 *                                 and end with a lowercase letter or number. Can contain lowercase letters,
 *                                 numbers, and hyphens. Maximum length is 30 characters.
 * @param int    $serverAccessType The access type of the Server.
 */
function create_server_sample(
    string $formattedParent,
    string $serverId,
    int $serverAccessType
): void {
    // Create a client.
    $cloudFtpClient = new CloudFtpClient();

    // Prepare the request message.
    $server = (new Server())
        ->setAccessType($serverAccessType);
    $request = (new CreateServerRequest())
        ->setParent($formattedParent)
        ->setServerId($serverId)
        ->setServer($server);

    // Call the API and handle any network failures.
    try {
        /** @var OperationResponse $response */
        $response = $cloudFtpClient->createServer($request);
        $response->pollUntilComplete();

        if ($response->operationSucceeded()) {
            /** @var Server $result */
            $result = $response->getResult();
            printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString());
        } else {
            /** @var Status $error */
            $error = $response->getError();
            printf('Operation failed with error data: %s' . PHP_EOL, $error->serializeToJsonString());
        }
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * Helper to execute the sample.
 *
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $formattedParent = CloudFtpClient::locationName('[PROJECT]', '[LOCATION]');
    $serverId = '[SERVER_ID]';
    $serverAccessType = AccessType::ACCESS_TYPE_UNSPECIFIED;

    create_server_sample($formattedParent, $serverId, $serverAccessType);
}
```

### Python


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/google-cloud-ftp/latest).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```python
# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
#   client as shown in:
#   https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import ftp_v1


def sample_create_server():
    # Create a client
    client = ftp_v1.CloudFtpClient()

    # Initialize request argument(s)
    server = ftp_v1.Server()
    server.access_type = "INTERNAL"

    request = ftp_v1.CreateServerRequest(
        parent="parent_value",
        server_id="server_id_value",
        server=server,
    )

    # Make the request
    operation = client.create_server(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)

```

### Ruby


To learn how to install and use the client library for Cloud FTP, see
[Cloud FTP client libraries](https://docs.cloud.google.com/cloud-ftp/reference/libraries.md.txt).


For more information, see the
[Cloud FTP Ruby API
reference documentation](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-ftp/latest).


To authenticate to Cloud FTP, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```ruby
require "google/cloud/ftp/v1"

##
# Snippet for the create_server call in the CloudFtp service
#
# This snippet has been automatically generated and should be regarded as a code
# template only. It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
# client as shown in https://cloud.google.com/ruby/docs/reference.
#
# This is an auto-generated example demonstrating basic usage of
# Google::Cloud::Ftp::V1::CloudFtp::Rest::Client#create_server.
#
def create_server
  # Create a client object. The client can be reused for multiple calls.
  client = Google::Cloud::Ftp::V1::CloudFtp::Rest::Client.new

  # Create a request. To set request fields, pass in keyword arguments.
  request = Google::Cloud::Ftp::V1::CreateServerRequest.new

  # Call the create_server method.
  result = client.create_server request

  # The returned object is of type Gapic::Operation. You can use it to
  # check the status of an operation, cancel it, or wait for results.
  # Here is how to wait for a response.
  result.wait_until_done! timeout: 60
  if result.response?
    p result.response
  else
    puts "No response received."
  end
end
```

<br />

## What's next

- [Add users to the server](https://docs.cloud.google.com/cloud-ftp/add-sftp-users.md.txt)
- [Connect to an external server](https://docs.cloud.google.com/cloud-ftp/connect-to-external-server.md.txt)