ruby aws sdk v2如何获取快照标记

时间:2016-09-28 20:23:39

标签: ruby amazon-web-services amazon-ec2 aws-sdk

p snapshot.tags的结果是:

[#<struct Aws::EC2::Types::Tag key="snapshot_expire", value="2016-09-23 08:30:12 +0000">]

如何在不使用小时的情况下获得标记键snapshot_expire的值:秒:分(2016-09-23)?

这是我的代码:

resp = ec2.describe_snapshots(owner_ids:['1234']) #.each do |snapshot|
resp.snapshots.each do |snapshot|
  p snapshot.snapshot_id
  p snapshot.tags
end

我知道常规的aws cli工具有这个选项。

谢谢!

1 个答案:

答案 0 :(得分:1)

参考:http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#describe_snapshots-instance_method

&#39;快照&#39;作为对象返回。您可以使用方法从对象获取特定的快照数据;请参阅上面的文档。通过调用snapshot.snapshot_id可以获得快照ID。标签是snapshot.tags。

require 'aws-sdk'
require 'date'
ec2 = Aws::EC2::Client.new(region: 'us-east-1')

snapshot_response = ec2.describe_snapshots(owner_ids:['xxx'])
snapshot_response.snapshots.each do |snapshot|
  tagkeyexpire = snapshot.tags.select{|tag| tag.key == 'snapshot_expire'}
  snapid = snapshot.snapshot_id
  expiredate = Date.parse(tagkeyexpire[0].value) unless tagkeyexpire == []
  puts "#{snapid} : #{expiredate}"
end

输出看起来像

snap-01234567 : 2016-09-30