2017-02-28 3 views

答えて

3

A-Ha!私はすでに答えがありましたが、これを理解するのは非常に難しいと感じました。

残念ながら、問題のファイルをソースURLからダウンロードしなければ、この作業を行うことができませんでした。

まずアプリでS3バケットを設定/設定/初期化子/ aws.rb

Aws.config.update({ 
    region: ENV['AWS_REGION'], 
    credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']), 
}) 

S3_BUCKET = Aws::S3::Resource.new.bucket(ENV['S3_BUCKET_NAME']) 

その後、私が作成したアプリ/労働/ aws_importer.rb

:だからここ

は、私が商品画像をアップロードしてみましょう

AwsImporter.new.upload_from_url(img_url, product_name) 
require 'aws-sdk' 

class AwsImporter 
    def upload_from_url (img_url, product_name) 
    image_file = open(img_url) // stage file for saving locally 
    local_image_path = product_name + ".jpg" // define filename and designate to root directory 
    IO.copy_stream(image_file, local_image_path) // download file to root directory 
    remote_image_path = "/products/#{product_name}/primary_image/#{local_image_path}" // set the desired url path in AWS 
    S3_BUCKET.object(remote_image_path).upload_file(local_image_path) // upload file to S3 
    File.delete(local_image_path) // then delete the local file copy 
    "https://s3.amazonaws.com/#{S3_BUCKET.name}/" + remote_image_path // return the new url path of the uploaded object. 
    end 
end 

は次にあなたがする必要があるすべては呼び出しです

これは、私たちが制御できる製品とimage_urlsをデータベースにシードするために、ウェブサイト全体を擦ったものです。

関連する問題