我有带有嵌入图像的 Rails 应用程序。我想要的是将这些图像上传到 s3 并从那里提供主题而不是形成原始源 在将其上传到 s3 之前我必须将 img 下载到我的服务器吗?
问问题
50 次
1 回答
1
简短回答:如果您正在抓取某人的内容,那么......是的,您需要先将文件拉下来,然后再上传到 S3。
长答案:如果其他站点(原始来源)正在与您合作,您可以为他们提供一个预签名 URL,他们可以使用该 URL 上传到您的 S3 存储桶。
来自亚马逊的文档:https ://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjectPreSignedURLRubySDK.html
#Uploading an object using a presigned URL for SDK for Ruby - Version 3.
require 'aws-sdk-s3'
require 'net/http'
s3 = Aws::S3::Resource.new(region:'us-west-2')
obj = s3.bucket('BucketName').object('KeyName')
# Replace BucketName with the name of your bucket.
# Replace KeyName with the name of the object you are creating or replacing.
url = URI.parse(obj.presigned_url(:put))
body = "Hello World!"
# This is the contents of your object. In this case, it's a simple string.
Net::HTTP.start(url.host) do |http|
http.send_request("PUT", url.request_uri, body, {
# This is required, or Net::HTTP will add a default unsigned content-type.
"content-type" => "",
})
end
puts obj.get.body.read
# This will print out the contents of your object to the terminal window.
于 2019-05-07T17:59:40.547 回答