2015-04-06 7 views
5

पर एक फ़ाइल अपलोड करना मैं एक फाइल अपलोड करने की कोशिश कर रहा हूं। एक सरल हैलो.txt। मैं दस्तावेज़ों का पालन कर रहा था, और मैं इसे अपनी बाल्टी में अपलोड करने में असमर्थ हूं।रूबी एसडीके का उपयोग करके अमेज़ॅन एस 3

# START AWS CLIENT 

s3 = Aws::S3::Resource.new 
bucket = s3.bucket(BUCKET_NAME) 

begin 

    s3.buckets[BUCKET_NAME].objects[KEY].write(:file => FILE_NAME) 
    puts "Uploading file #{FILE_NAME} to bucket #{BUCKET_NAME}." 

    bucket.objects.each do |obj| 
    puts "#{obj.key} => #{obj.etag}" 
    end 

rescue Aws::S3::Errors::ServiceError 
    # rescues all errors returned by Amazon Simple Storage Service 
end 

मैं पीछा कर रहा था http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpRuby.html

त्रुटि:

➜ s3-tester ruby main.rb /Users/.rvm/gems/ruby-2.1.1/gems/aws-sdk-resources-2.0.34/lib/aws-sdk-resources/collection.rb:79:in 'method_missing: undefined method []' for ' <Aws::Resources::Collection:0x000001031e5100> (NoMethodError)' from 'main.rb:18:in <main> '

+0

क्या मुद्दा है चलाकर my_bucket को simple.txt अपलोड कर सकते हैं upload.rb को बचाया? –

+0

मुझे एक [] कोई विधि त्रुटि नहीं मिली –

+0

ठीक है ... प्रश्न में त्रुटि डालें। –

उत्तर

2
client = Aws::S3::Client.new(region: 'us-east-1') 
resource = Aws::S3::Resource.new(client: client) 
bucket = resource.bucket(BUCKET_NAME) 
begin 
    # s3.buckets[BUCKET_NAME].objects[KEY].write(:file => FILE_NAME) 
    # puts "Uploading file #{FILE_NAME} to bucket #{BUCKET_NAME}." 

    bucket.objects.each do |o| 
    puts o.key 
    end 

rescue Aws::S3::Errors::ServiceError 
    # rescues all errors returned by Amazon Simple Storage Service 
end 
+0

मुझे S3 से कनेक्ट करने में कोई समस्या नहीं है। मैंने अपना क्षेत्र और क्रेडिट निर्धारित किया। मैं एक फाइल अपलोड नहीं कर सकता। एक साधारण फ़ाइल .. –

+0

क्या आप क्लाइंट के रूप में s3 को परिभाषित करना चाहते हैं, संसाधन नहीं –

+0

मैंने इसे दोनों तरीकों से आजमाया है। –

7

प्राथमिक मुद्दा आप एडब्ल्यूएस एसडीके रूबी स्थापित करने के लिए का संस्करण 2 है कि है, लेकिन आप संदर्भित कर रहे हैं संस्करण 1 के लिए प्रलेखन। संस्करण 2 प्रलेखन यहां पाया जा सकता:

s3 = Aws::S3::Resource.new 
bucket = s3.bucket(BUCKET_NAME) 

begin 

    bucket.object(KEY).upload_file(FILENAME) 
    puts "Uploading file #{FILE_NAME} to bucket #{BUCKET_NAME}." 

    bucket.objects.each do |obj| 
    puts "#{obj.key} => #{obj.etag}" 
    end 

rescue Aws::S3::Errors::ServiceError 
    # rescues all errors returned by Amazon Simple Storage Service 
end 

प्राथमिक अंतर:

http://docs.aws.amazon.com/sdkforruby/api/index.html

संस्करण 2 उपयोग करने के लिए अपने उदाहरण को अपडेट करने के

  • संस्करण 1 इस्तेमाल किया # [] किसी ऑब्जेक्ट को इसकी कुंजी से संदर्भित करने के लिए संग्रह पर विधि। संस्करण 2 में दो विधियां हैं, #objects() और #object(key)। उत्तरार्द्ध गेटटर है। पूर्व बाल्टी में सभी वस्तुओं की गणना करता है।
  • संस्करण 2 में एक विशेष #upload_file विधि है जो डिस्क से ऑब्जेक्ट अपलोड करने का प्रबंधन करती है। यह संस्करण 1 से #write के समान है, लेकिन यह समानांतर में बड़े ऑब्जेक्ट भागों को अपलोड करने के लिए एकाधिक थ्रेड का भी उपयोग कर सकता है।
1

मैंने निम्नलिखित की तरह एक स्क्रिप्ट का उपयोग किया जो एक नई बाल्टी बनायेगा यदि यह अस्तित्व में नहीं है और फिर उसमें चुनी गई फ़ाइल अपलोड करें।

#!/usr/bin/env ruby 
# 

require 'rubygems' 
require 'aws-sdk' 

bucket_name = ARGV[0] 
file_name = ARGV[1] 


# Get an instance of the S3 interface. 
s3 = Aws::S3::Client.new(region: 'us-east-1') 

key = File.basename(file_name) 
resp = s3.list_buckets() 
buckets = resp.data.buckets 

if buckets.select { |b| b.name == bucket_name }.length == 0 
    puts 'creating bucket' 
    s3.create_bucket(bucket: bucket_name) 
end 

puts "Uploading file #{file_name} to bucket #{bucket_name}..." 

# Upload a file. 
s3.put_object(
    :bucket => bucket_name, 
    :key => key, 
    :body => IO.read(file_name) 
) 

अगर आपको लगता है कि आप

$ ruby upload.rb my_bucket simple.txt