gpt4 book ai didi

javascript - 使用 Uppy 槽神社上传图像返回 405 方法不允许

转载 作者:行者123 更新时间:2023-11-28 17:11:15 24 4
gpt4 key购买 nike

我正在尝试使用 Ruby On Rails 作为我的后端,以及像 ReactOnRails 和 Shrine 这样的 gem,此外,我使用包 Uppy 将图像上传到 AWS S3

到目前为止,一切工作正常,直到我尝试通过 Uppy 上传图像,Uppy 应该通过后端传输 AWS 凭证,这样我就不会从前端暴露我的 key 。

React 组件的片段

.use(AwsS3, {
limit: 5,
// serverUrl: 'https://uppy-companion.my-app.com/',
strings: {
preparingUpload: 'Preparing upload...'
},
getUploadParameters(file) {
return fetch('/presign?filename=' + file.name, {
method: 'post',
// Send and receive JSON.
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({
filename: file.name,
contentType: file.type
})
}).then((response) => {
// Parse the JSON response.
return response.json()
}).then((data) => {
// Return an object in the correct shape.
return {
method: data.method,
url: data.url,
fields: data.fields
}
}).catch((error) => console.log(error))
}
});

我的路线是

mount Shrine.presign_endpoint(:cache) => '/presign'

最后是我的 shrine.rb 文件

require 'shrine'

if Rails.env.production?
require 'shrine/storage/s3'

s3_options = {
access_key_id: Rails.application.credentials.aws[:s3_access_key_id],
secret_access_key: Rails.application.credentials.aws[:s3_secret_access_key],
region: Rails.application.credentials.aws[:s3_region],
bucket: Rails.application.credentials.aws[:s3_bucket]
}

Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: 'cache', **s3_options),
store: Shrine::Storage::S3.new(prefix: "store", **s3_options)
}
else
require 'shrine/storage/file_system'

Shrine.storages = {
cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'),
store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads')
}
end

Shrine.plugin :activerecord
Shrine.plugin :backgrounding
Shrine.plugin :logging
Shrine.plugin :determine_mime_type
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
#TODO: docs
Shrine.plugin :presign_endpoint if Rails.env.production?
Shrine.plugin :upload_endpoint if !Rails.env.production?

Shrine::Attacher.promote { |data| PromoteJob.perform_async(data) }
Shrine::Attacher.delete { |data| DeleteJob.perform_async(data) }

我的 AWS S3 Cross 配置

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
<AllowedHeader>x-amz-date</AllowedHeader>
<AllowedHeader>x-amz-content-sha256</AllowedHeader>
<AllowedHeader>content-type</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
</CORSRule>
</CORSConfiguration>

当我通过 uppy.js 上传文件时,我只是不知道如何解决这个问题。

最佳答案

Shrine 的 presign 端点响应 GET 请求,而不是 POST,因此您需要执行

getUploadParameters(file) {
return fetch('/presign?filename=' + file.name, {
method: 'get',
...

顺便说一句,如果您将 presign 端点挂载在 /s3/params 上:

Rails.application.routes.draw do
mount Shrine.presign_endpoint(:cache) => "/s3/params"
end

您不需要任何 fetch() 逻辑,您只需将 Uppy 指向您的应用即可,它会自动使用 /s3/params 并执行以下操作所有的抓取工作都为您完成:

uppy.use(AwsS3, {
companionUrl: '/',
...
})

关于javascript - 使用 Uppy 槽神社上传图像返回 405 方法不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com