如何在Ruby中执行相当于's3cmd setacl --acl-grant = read:82b82d .. s3:// somebucket / ..'的操作?

时间:2011-07-07 19:44:42

标签: ruby amazon-s3 amazon-cloudfront s3cmd

你怎么做相同的:

s3cmd setacl --acl-grant=read:82b82d14a8d011e09d86001cc029a3688cdd635ea8d011e0b499001cc029a3689052a4f4a8d011e0bd25001cc029a368 s3://somebucket/some/path/to/file
在Ruby中

? (最好使用'aws-s3'宝石)

===编辑===

正如Soren在下面所建议的,类似的东西应该有效:

grant = AWS::S3::ACL::Grant.new
grant.permission = 'READ'
grantee = AWS::S3::ACL::Grantee.new
grantee.id = '82b82d14a8d011e09d86001cc029a3688cdd635ea8d011e0b499001cc029a3689052a4f4a8d011e0bd25001cc029a368'
grant.grantee = grantee
acl = AWS::S3::S3Object.acl('some/path/to/file', 'somebucket')
acl.grants << grant
AWS::S3::S3Object.acl 'some/path/to/file', 'somebucket', acl 

然而,这不起作用,我收到以下错误:

  

您提供的XML不是   格式良好或未验证   反对我们发布的架构   (AWS :: S3 :: MalformedACLError)

任何想法如何使这项工作?

4 个答案:

答案 0 :(得分:1)

我无法使用'aws-s3'宝石,但它确实适用于'rightscale_aws'宝石:

require 'right_aws'

s3     = RightAws::S3.new(access_key, secret_key, {:logger => Logger.new('/dev/null')})
bucket =  s3.bucket('somebucket')

bucket.put 'some/path/to/file', open('/tmp/myfile')
access_id = '82b82d14a8d011e09d86001cc029a3688cdd635ea8d011e0b499001cc029a3689052a4f4a8d011e0bd25001cc029a368'
key = bucket.key('some/path/to/file')
RightAws::S3::Grantee.new(key, access_id, ['READ'], :apply)

答案 1 :(得分:1)

我确实在同样的错误上挣扎,文档在这一点上很差,你必须看看Grantee class doc

要设置被授权者ID,您需要指定:

grantee.type = "CanonicalUser"
grantee.name = "aName"
grantee.id = '82b82d14a8d011e09d86001cc029a3688cdd635ea8d011e0b499001cc029a3689052a4f4a8d011e0bd25001cc029a368'

这解决了您有错误的XML错误

希望这有帮助, 文森特

答案 2 :(得分:1)

我自己遇到了同样的错误。似乎要解决它,你需要首先获取对象的策略,然后修改它,并将修改后的ACL应用到对象上。

我看到的一个区别是你没有明确地将类型定义为CanonicalUser。另一个问题可能是您正在阅读其ACL的对象未授予您这样做的权限(您缺少READ_ACP权限)。

policy = AWS::S3::S3Object.acl('object_in_somebucket', 'somebucket')
grantee = AWS::S3::ACL::Grantee.new
grantee.type = 'CanonicalUser'
grantee.id = '82b82d14a8d011e09d86001cc029a3688cdd635ea8d011e0b499001cc029a3689052a4f4a8d011e0bd25001cc029a368'

grant = AWS::S3::ACL::Grant.new
grant.permission = 'READ'
grant.grantee = grantee
policy.grants << grant
AWS::S3::S3Object.acl('object_in_somebucket', 'somebucket', policy)

答案 3 :(得分:-1)

Ruby实现(您在此处http://amazon.rubyforge.org/ http://amazon.rubyforge.org/doc/)应该适合您。