删除 SFTP 服务器

本文档介绍了如何删除 SFTP 服务器。

准备工作

所需的角色

如需获得删除 SFTP 服务器所需的权限,请让您的管理员为您授予项目的Cloud FTP Admin (roles/ftp.admin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。

此预定义角色包含删除 SFTP 服务器所需的 ftp.servers.delete 权限。

您也可以使用自定义角色或其他预定义角色来获取此权限。

删除服务器

gcloud

如需删除 SFTP 服务器,请运行 gcloud alpha storage ftp servers delete 命令。

在使用以下任何命令数据之前, 请进行以下替换:

  • SERVER_ID:SFTP 服务器 ID。
  • LOCATION_ID:服务器的位置,例如 us-west1。

执行以下命令:

Linux、macOS 或 Cloud Shell

gcloud alpha storage ftp servers delete SERVER_ID --location=LOCATION_ID

Windows (PowerShell)

gcloud alpha storage ftp servers delete SERVER_ID --location=LOCATION_ID

Windows (cmd.exe)

gcloud alpha storage ftp servers delete SERVER_ID --location=LOCATION_ID
删除服务器大约需要 10 分钟。

REST

如需删除 SFTP 服务器,请使用 servers.delete 方法。

在使用任何请求数据之前, 请进行以下替换:

  • PROJECT_ID:项目 Google Cloud ID。
  • LOCATION_ID:服务器的位置,例如 us-west1。
  • SERVER_ID:SFTP 服务器 ID。

HTTP 方法和网址:

DELETE https://ftp.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/servers/SERVER_ID

如需发送您的请求,请展开以下选项之一:

响应会识别 长时间运行的操作。 删除服务器大约需要 10 分钟。

客户端库

C#

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP C# API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。

using Google.Cloud.Ftp.V1;
using Google.LongRunning;
using Google.Protobuf.WellKnownTypes;
using System.Threading.Tasks;

public sealed partial class GeneratedCloudFtpClientSnippets
{
    /// <summary>Snippet for DeleteServerAsync</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 DeleteServerAsync()
    {
        // Create client
        CloudFtpClient cloudFtpClient = await CloudFtpClient.CreateAsync();
        // Initialize request argument(s)
        string name = "projects/[PROJECT]/locations/[LOCATION]/servers/[SERVER]";
        // Make the request
        Operation<Empty, OperationMetadata> response = await cloudFtpClient.DeleteServerAsync(name);

        // Poll until the returned long-running operation is complete
        Operation<Empty, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();
        // Retrieve the operation result
        Empty 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<Empty, OperationMetadata> retrievedResponse = await cloudFtpClient.PollOnceDeleteServerAsync(operationName);
        // Check if the retrieved long-running operation has completed
        if (retrievedResponse.IsCompleted)
        {
            // If it has completed, then access the result
            Empty retrievedResult = retrievedResponse.Result;
        }
    }
}

Go

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP Go API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。


//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.DeleteServerRequest{
		// TODO: Fill request struct fields.
		// See https://pkg.go.dev/cloud.google.com/go/ftp/apiv1/ftppb#DeleteServerRequest.
	}
	op, err := c.DeleteServer(ctx, req)
	if err != nil {
		// TODO: Handle error.
	}

	err = op.Wait(ctx)
	if err != nil {
		// TODO: Handle error.
	}
}

Java

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP Java API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。

import com.google.api.core.ApiFuture;
import com.google.cloud.ftp.v1.CloudFtpClient;
import com.google.cloud.ftp.v1.DeleteServerRequest;
import com.google.cloud.ftp.v1.ServerName;
import com.google.longrunning.Operation;

public class AsyncDeleteServer {

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

  public static void asyncDeleteServer() 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()) {
      DeleteServerRequest request =
          DeleteServerRequest.newBuilder()
              .setName(ServerName.of("[PROJECT]", "[LOCATION]", "[SERVER]").toString())
              .build();
      ApiFuture<Operation> future = cloudFtpClient.deleteServerCallable().futureCall(request);
      // Do something.
      future.get();
    }
  }
}

Node.js

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP Node.js API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。

/**
 * 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. Name of the resource
 */
// const name = 'abc123'

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

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

async function callDeleteServer() {
  // Construct request
  const request = {
    name,
  };

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

callDeleteServer();

PHP

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP PHP API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。

use Google\ApiCore\ApiException;
use Google\ApiCore\OperationResponse;
use Google\Cloud\Ftp\V1\Client\CloudFtpClient;
use Google\Cloud\Ftp\V1\DeleteServerRequest;
use Google\Rpc\Status;

/**
 * Deletes a single Server.
 *
 * @param string $formattedName Name of the resource
 *                              Please see {@see CloudFtpClient::serverName()} for help formatting this field.
 */
function delete_server_sample(string $formattedName): void
{
    // Create a client.
    $cloudFtpClient = new CloudFtpClient();

    // Prepare the request message.
    $request = (new DeleteServerRequest())
        ->setName($formattedName);

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

        if ($response->operationSucceeded()) {
            printf('Operation completed successfully.' . PHP_EOL);
        } 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
{
    $formattedName = CloudFtpClient::serverName('[PROJECT]', '[LOCATION]', '[SERVER]');

    delete_server_sample($formattedName);
}

Python

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP Python API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。

# 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_delete_server():
    # Create a client
    client = ftp_v1.CloudFtpClient()

    # Initialize request argument(s)
    request = ftp_v1.DeleteServerRequest(
        name="name_value",
    )

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

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

    response = operation.result()

    # Handle the response
    print(response)

Ruby

如需了解如何安装和使用 Cloud FTP 的客户端库,请参阅 Cloud FTP 客户端库。 如需了解详情,请参阅 Cloud FTP Ruby API 参考文档。

如需向 Cloud FTP 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证。

require "google/cloud/ftp/v1"

##
# Snippet for the delete_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#delete_server.
#
def delete_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::DeleteServerRequest.new

  # Call the delete_server method.
  result = client.delete_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