AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK或CreateTrail
一起使用 CLI
以下代码示例演示如何使用 CreateTrail
。
- C++
-
- SDK对于 C++
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 // Routine which creates an AWS CloudTrail trail. /*! \param trailName: The name of the CloudTrail trail. \param bucketName: The Amazon S3 bucket designate for publishing logs. \param clientConfig: Aws client configuration. \return bool: Function succeeded. */ bool AwsDoc::CloudTrail::createTrail(const Aws::String trailName, const Aws::String bucketName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::CloudTrail::CloudTrailClient trailClient(clientConfig); Aws::CloudTrail::Model::CreateTrailRequest request; request.SetName(trailName); request.SetS3BucketName(bucketName); Aws::CloudTrail::Model::CreateTrailOutcome outcome = trailClient.CreateTrail( request); if (outcome.IsSuccess()) { std::cout << "Successfully created trail " << trailName << std::endl; } else { std::cerr << "Failed to create trail " << trailName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
有关API详细信息,请参阅 “AWS SDK for C++ API参考 CreateTrail” 中的。
-
- CLI
-
- AWS CLI
-
创建跟踪
以下
create-trail
命令创建名为的多区域跟踪Trail1
并指定一个 S3 存储桶:aws cloudtrail create-trail --name
Trail1
--s3-bucket-namemy-bucket
--is-multi-region-trail输出:
{ "IncludeGlobalServiceEvents": true, "Name": "Trail1", "TrailARN": "arn:aws:cloudtrail:us-west-2:123456789012:trail/Trail1", "LogFileValidationEnabled": false, "IsMultiRegionTrail": true, "S3BucketName": "my-bucket" }
-
有关API详细信息,请参阅 “CreateTrail AWS CLI
命令参考”。
-
- PowerShell
-
- 用于 PowerShell
-
示例 1:创建一个使用存储桶 “mycloudtrailbucket” 存储日志文件的跟踪。
New-CTTrail -Name "awscloudtrail-example" -S3BucketName "amzn-s3-demo-bucket"
示例 2:创建一个使用存储桶 “mycloudtrailbucket” 存储日志文件的跟踪。代表日志的 S3 对象的通用键前缀为 “mylogs”。当新日志传送到存储桶时,将向SNS主题 “mlog-deliverytopic” 发送通知。此示例使用 splatting 向 cmdlet 提供参数值。
$params = @{ Name="awscloudtrail-example" S3BucketName="amzn-s3-demo-bucket" S3KeyPrefix="mylogs" SnsTopicName="mlog-deliverytopic" } New-CTTrail @params
-
有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考CreateTrail中的。
-
- Ruby
-
- SDK对于 Ruby
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 require 'aws-sdk-cloudtrail' # v2: require 'aws-sdk' require 'aws-sdk-s3' require 'aws-sdk-sts' def create_trail_example(s3_client, sts_client, cloudtrail_client, trail_name, bucket_name) resp = sts_client.get_caller_identity({}) account_id = resp.account # Attach policy to an Amazon Simple Storage Service (S3) bucket. s3_client.create_bucket(bucket: bucket_name) begin policy = { 'Version' => '2012-10-17', 'Statement' => [ { 'Sid' => 'AWSCloudTrailAclCheck20150319', 'Effect' => 'Allow', 'Principal' => { 'Service' => 'cloudtrail.amazonaws.com' }, 'Action' => 's3:GetBucketAcl', 'Resource' => "arn:aws:s3:::#{bucket_name}" }, { 'Sid' => 'AWSCloudTrailWrite20150319', 'Effect' => 'Allow', 'Principal' => { 'Service' => 'cloudtrail.amazonaws.com' }, 'Action' => 's3:PutObject', 'Resource' => "arn:aws:s3:::#{bucket_name}/AWSLogs/#{account_id}/*", 'Condition' => { 'StringEquals' => { 's3:x-amz-acl' => 'bucket-owner-full-control' } } } ] }.to_json s3_client.put_bucket_policy( bucket: bucket_name, policy: policy ) puts "Successfully added policy to bucket #{bucket_name}" end begin cloudtrail_client.create_trail({ name: trail_name, # required s3_bucket_name: bucket_name # required }) puts "Successfully created trail: #{trail_name}." rescue StandardError => e puts "Got error trying to create trail #{trail_name}:\n #{e}" puts e exit 1 end
-
有关API详细信息,请参阅 “AWS SDK for Ruby API参考 CreateTrail” 中的。
-
操作
DeleteTrail