本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
CloudTrail 使用适用于 Ruby 的 SDK 的示例
以下代码示例向您展示了如何使用with来执行操作和实现常见场景 CloudTrail。 Amazon SDK for Ruby
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。
场景 是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。
每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示了如何使用 CreateTrail
。
- 适用于 Ruby 的 SDK
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 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 的详细信息,请参阅 Amazon SDK for Ruby API 参考CreateTrail中的。
-
以下代码示例演示了如何使用 DeleteTrail
。
- 适用于 Ruby 的 SDK
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 client.delete_trail({ name: trail_name # required }) puts "Successfully deleted trail: " + trail_name rescue StandardError => err puts "Got error trying to delete trail: " + trail_name + ":" puts err exit 1 end
-
有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考DeleteTrail中的。
-
以下代码示例演示了如何使用 ListTrails
。
- 适用于 Ruby 的 SDK
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 require "aws-sdk-cloudtrail" # v2: require 'aws-sdk' def describe_trails_example(client) resp = client.describe_trails({}) puts "Found #{resp.trail_list.count} trail(s)." resp.trail_list.each do |trail| puts "Name: " + trail.name puts "S3 bucket name: " + trail.s3_bucket_name puts end
-
有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考ListTrails中的。
-
以下代码示例演示了如何使用 LookupEvents
。
- 适用于 Ruby 的 SDK
-
注意
还有更多相关信息 GitHub。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 require "aws-sdk-cloudtrail" # v2: require 'aws-sdk' # @param [Object] client def lookup_events_example(client) resp = client.lookup_events puts "Found #{resp.events.count} events:" resp.events.each do |e| puts "Event name: #{e.event_name}" puts "Event ID: #{e.event_id}" puts "Event time: #{e.event_time}" puts "Resources:" e.resources.each do |r| puts " Name: #{r.resource_name}" puts " Type: #{r.resource_type}" puts "" end end end
-
有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考LookupEvents中的。
-