2013-05-12 113 views
4

我想上传整个目录使用Ruby AWS-SDK宝石S3。我首先试图将它分解成更小的问题,所以我现在试图做的只是创建一个文件夹,然后将文件放在该文件夹内。我知道技术上s3没有文件夹,而是对象。我知道你可以拥有类似目录的结构。我无法找到如何做到这一点的在线任何与文档不提了很多关于目录结构除了与AWS阅读:: S3 ::树如何在s3上使用ruby aws-sdk创建目录结构?

这是我在创建一个文件夹,然后把当前的尝试文件到文件夹:

#created an object called test 
obj = bucket.objects.create('test', 'data') 

#this is the path to a css file that I am uploading. 
path_to_file = './directory/mobile/player.css' 

#writing file to test object 
obj.write(Pathname.new(path_to_file)) 

这实际上在做什么是写入css文件来测试。我想要它做的是在名为test的文件夹内创建一个css文件。

我相信我误解对象与目录的方式。任何人都可以发现我要出错的地方,或者指出我的方向正确吗?

+1

看起来有在Amazon S3中没有目录结构:http://serverfault.com/a/435828,http://stackoverflow.com/a/11503643/1008230 – fankt 2013-05-13 08:12:25

+4

,我发现,你可以只创建一个路径的对象有一个类似目录的结构。 bucket.objects.create(文件夹/测试/ player.css)。当你查看bucket as_tree时,它们将显示为目录。 – 2013-05-13 15:43:31

+0

@SpencerCooley您能详细说明您找到的解决方案吗? – Noz 2014-01-03 19:09:14

回答

0

您可以使用下面的代码:

# Instantiate the S3 client with your AWS credentials 
s3 = AWS::S3.new(
    :access_key_id => 'Aws_Access_Key', 
    :secret_access_key => 'Aws_Secret_Key' 
) 

# If you want to create bucket 
# bucketName: name of the bucket 
bucket = s3.buckets.create('bucketName', :acl => :public_read) 

# push file to s3 
# bucketName: Amazon Bucket Name 
# key: The file location that you want to create for your file. 
# e.g, key = "user/appName/myapp.zip" 
# File_To_Save: The location of your file. 

obj = bucket.objects['key'] 
obj.write(:file => file_name) 
puts obj.public_url 

# This key describes the directory structure for your file 
+0

不知道如何与AWS-SDK的V2为此桶文件的目录?看起来好像很多已经改变了,它不再那么简单了。 – mmichael 2015-03-22 22:26:01

相关问题