3

How can you do the equivalent of:

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

in Ruby? (preferably by using the 'aws-s3' gem)

=== Edit ===

As Soren suggests below, something similar to this should work:

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 

However that does not work, I get the following error:

The XML you provided was not well-formed or did not validate against our published schema (AWS::S3::MalformedACLError)

Any ideas how to make this work?

4

4 回答 4

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)
于 2012-06-02T06:39:04.917 回答
1

我在同样的错误上苦苦挣扎,在这一点上文档很差,你必须查看Grantee 类文档

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

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

这解决了您遇到的格式错误的 XML 错误

希望这会有所帮助,文森特

于 2012-03-08T06:59:10.747 回答
1

我无法让它与“aws-s3”gem 一起使用,但它确实可以与“rightscale_aws”gem 一起使用:

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)
于 2011-07-08T10:06:05.187 回答
-1

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

于 2011-07-07T19:50:34.420 回答