mod*_*odi 5 amazon-s3 amazon-ec2 amazon-web-services laravel vue.js
我有 2 个应用程序,1 个用于仪表板 (VueJS),1 个用于 api (Laravel)...
在我的 laravel 中,我有一个 api 函数,可以将图像上传到我的 s3 存储桶:
class ImageController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['uploadimage']]);
$this->middleware('cors');
}
public function uploadimage(Request $request) {
$disk = Storage::disk('s3');
$disk->put($request->path.'/'.$request->file_name, base64_decode($request->file));
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的 VueJS 应用程序中通过以下方式调用了这个函数:
uploadImage(ext) {
const d = new Date();
const self = this;
const f = new URLSearchParams();
f.append('user_id', localStorage.getItem('id'))
f.append('file_name', 'ProfilePicture-'+d.getTime()+'-'+localStorage.getItem('id')+'.'+ext);
f.append('path', 'public/images');
f.append('file', this.image_crop[0].replace(/^data:image\/[a-z]+;base64,/, ''));
this.axios.post('uploadimage'), f, {
headers: ''
}).then(response => {
const status = response.data.status;
if (status == 1) {
Toast._success(self, 'Success!', 'You have successfully updated your profile picture.');
} else {
if (status == 0) {
Toast._error(self, 'Oops!', 'Something went wrong. Please try again later.');
}
}
console.log('res ', response);
}).catch(err => {
console.log('err ', err);
})
}
Run Code Online (Sandbox Code Playgroud)
这在我使用本地 db(xampp) 的本地开发环境中运行良好。
但是当我使用 RDS mysql db 在我的 AWS ec2 ubuntu(apache) 中部署Laravel and VueJS App并上传图像时,出现此错误
InvalidArgumentException: The PutObject operation requires non-empty parameters: Bucket in file /var/www/api.myapp.com/public_html/myapp_be/vendor/aws/aws-sdk-php/src/InputValidationMiddleware.php
Run Code Online (Sandbox Code Playgroud)
顺便说一句,api.myapp.com就是Laravel所在的地方。
我是新手Laravel,AWS我只是找不到与此问题相关的任何内容。知道如何解决这个问题吗?
mod*_*odi 10
解决了问题,只是我忘记s3填充filesystems.php.
要解决该问题,请按照以下步骤操作:
1.转到filesystems.php
2.找到disk数组,里面就是s3数组
3.在s3数组中,使用与 中相同的值填充以下属性.env:
'disks' => [
'local' => [
...
],
'public' => [
...
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID', 'Same with the AWS_ACCESS_KEY_ID value in the .env file'),
'secret' => env('AWS_SECRET_ACCESS_KEY', 'Same with the AWS_ACCESS_KEY_ID value in the .env file'),
'region' => env('AWS_DEFAULT_REGION', 'Same with the AWS_DEFAULT_REGION value in the .env file'),
'bucket' => env('AWS_BUCKET', 'Your s3 bucket'),
'url' => env('AWS_URL'), //You can keep this as is
'endpoint' => env('AWS_ENDPOINT'), //You can keep this as is
],
],
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助遇到同样问题的人。
| 归档时间: |
|
| 查看次数: |
20772 次 |
| 最近记录: |