Skip to content

Commit

Permalink
figured out how to encode S3 keys for URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomandersen committed Jun 16, 2010
1 parent 205c917 commit 04b494b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/awsbase/right_awsbase.rb
Expand Up @@ -210,6 +210,7 @@ def self.escape_sig(raw)
# CGI::escape will escape characters in the protocol, host, and port
# sections of the URI. Only target chars in the query
# string should be escaped.
# at the present time this is only called from SQS, so perhaps should be moved there and made to follow the SQS documentation explicitely
def self.URLencode(raw)
e = URI.escape(raw)
e.gsub(/\+/, "%2b")
Expand Down
25 changes: 18 additions & 7 deletions lib/s3/right_s3_interface.rb
Expand Up @@ -880,13 +880,24 @@ def list_bucket_link(bucket, options=nil, expires=nil, headers={})
rescue
on_exception
end


# Encodes key for inclusion in URLs, etc.
#
# self.encodeKey('s3 key with on/a/path with arbitrary unicode chars, such as this?<>!(or this)') #=> encoded string suitable for framing
#
def self.encodeKey(key)
# EG: CGI escape is str.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }, but we can leave in / and some others for easier reading
# esacpe all characters except a-Z, 0-9, and - _ . ! ~ *' ( ) /
key.gsub(/[^a-zA-Z0-9\-_.!~*'()\/]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
end


# Generates link for 'PutObject'.
#
# s3.put_link('my_awesome_bucket',key, object) #=> url string
#
def put_link(bucket, key, data=nil, expires=nil, headers={})
generate_link('PUT', headers.merge(:url=>"#{bucket}/#{AwsUtils::URLencode key}", :data=>data), expires)
generate_link('PUT', headers.merge(:url=>"#{bucket}/#{S3Interface::encodeKey key}", :data=>data), expires)
rescue
on_exception
end
Expand All @@ -904,7 +915,7 @@ def put_link(bucket, key, data=nil, expires=nil, headers={})
#
# see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/VirtualHosting.html
def get_link(bucket, key, expires=nil, headers={})
generate_link('GET', headers.merge(:url=>"#{bucket}/#{AwsUtils::URLencode key}"), expires)
generate_link('GET', headers.merge(:url=>"#{bucket}/#{S3Interface::encodeKey key}"), expires)
rescue
on_exception
end
Expand All @@ -914,7 +925,7 @@ def get_link(bucket, key, expires=nil, headers={})
# s3.head_link('my_awesome_bucket',key) #=> url string
#
def head_link(bucket, key, expires=nil, headers={})
generate_link('HEAD', headers.merge(:url=>"#{bucket}/#{AwsUtils::URLencode key}"), expires)
generate_link('HEAD', headers.merge(:url=>"#{bucket}/#{S3Interface::encodeKey key}"), expires)
rescue
on_exception
end
Expand All @@ -924,7 +935,7 @@ def head_link(bucket, key, expires=nil, headers={})
# s3.delete_link('my_awesome_bucket',key) #=> url string
#
def delete_link(bucket, key, expires=nil, headers={})
generate_link('DELETE', headers.merge(:url=>"#{bucket}/#{AwsUtils::URLencode key}"), expires)
generate_link('DELETE', headers.merge(:url=>"#{bucket}/#{S3Interface::encodeKey key}"), expires)
rescue
on_exception
end
Expand All @@ -935,7 +946,7 @@ def delete_link(bucket, key, expires=nil, headers={})
# s3.get_acl_link('my_awesome_bucket',key) #=> url string
#
def get_acl_link(bucket, key='', headers={})
return generate_link('GET', headers.merge(:url=>"#{bucket}/#{AwsUtils::URLencode key}?acl"))
return generate_link('GET', headers.merge(:url=>"#{bucket}/#{S3Interface::encodeKey key}?acl"))
rescue
on_exception
end
Expand All @@ -945,7 +956,7 @@ def get_acl_link(bucket, key='', headers={})
# s3.put_acl_link('my_awesome_bucket',key) #=> url string
#
def put_acl_link(bucket, key='', headers={})
return generate_link('PUT', headers.merge(:url=>"#{bucket}/#{AwsUtils::URLencode key}?acl"))
return generate_link('PUT', headers.merge(:url=>"#{bucket}/#{S3Interface::encodeKey key}?acl"))
rescue
on_exception
end
Expand Down

0 comments on commit 04b494b

Please sign in to comment.