简体   繁体   English

如何在s3上使用ruby aws-sdk创建目录结构?

[英]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.

相关问题 无法使用 aws-sdk ruby​​ gem 创建存储桶。 Aws::S3::Errors::SignatureDoesNotMatch - Can't create bucket using aws-sdk ruby gem. Aws::S3::Errors::SignatureDoesNotMatch 如何在Ruby中对S3和AWS-SDK使用经过身份验证的用户 - How to use an authenticated user with S3 and AWS-SDK in Ruby 无法使用ruby aws-sdk v2在S3中创建存储桶 - Cannot create a bucket in S3 using ruby aws-sdk v2 如何在 ruby​​ on rails 中使用 AWS-SDK gem 列出 s3 文件夹中的所有文件 - How to list all files in an s3 folder using AWS-SDK gem in ruby on rails 有没有一种方法可以使用mv命令通过aws-sdk ruby​​ gem在S3存储桶中移动目录? - Is there a way to use mv command to move directory in S3 bucket with aws-sdk ruby gem? AWS S3红宝石AWS-SDK文件传输器使用关键云铸造 - aws s3 ruby aws-sdk file transer using pivotal cloud foundry 使用aws-sdk ruby​​ gem检查AWS S3中是否存在新创建的文件 - Checking if a newly created file exists in AWS S3 using aws-sdk ruby gem 使用Ruby aws-sdk选择并使用AWS s3对象密钥的一部分 - Select and use part of an AWS s3 object key using Ruby aws-sdk 使用aws-sdk - > 2和ruby更改s3文件访问权限 - Change s3 file access with aws-sdk -> 2 and ruby 使用aws-sdk在rails中为ruby下载S3文件(对象) - Downloading S3 files(objects) using aws-sdk for ruby in rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM