[英]How do you create a directory structure using the ruby aws-sdk on s3?
I am trying to upload an entire directory to s3 using the ruby aws-sdk gem. 我正在尝试使用ruby aws-sdk gem将整个目录上传到s3。 I am first trying to break it down in to smaller problems so what I am currently trying to do is simply create one single folder and then place a file inside of that folder.
我首先尝试将其分解为较小的问题,因此我目前要做的只是创建一个文件夹,然后将文件放在该文件夹中。 I know that technically s3 does not have folders, but objects.
我知道技术上s3没有文件夹,而是对象。 I do know that you can have a directory-like structure though.
我知道你可以拥有类似目录的结构。 I can not find anything on how to do this online and the docs don't mention a lot about directory structure besides reading with AWS::S3::Tree
我找不到任何关于如何在线进行此操作的内容,除了使用AWS :: S3 :: Tree阅读之外,文档中没有提及很多关于目录结构的内容
This is my current attempt at creating a folder and then adding a file to the folder: 这是我当前尝试创建文件夹然后将文件添加到该文件夹:
#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))
What this is actually doing is writing the css file to test. 这实际上做的是编写css文件进行测试。 What I want it to do is create a css file inside a folder named test.
我想要它做的是在名为test的文件夹中创建一个css文件。
I am sure I am misunderstanding the way objects are related to directories. 我确信我误解了对象与目录的关联方式。 Can anyone spot where I am going wrong or point me in the correct direction?
任何人都可以发现我出错的地方或指向正确的方向吗?
You can use the following code: 您可以使用以下代码:
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.