【问题标题】:Use Axios defaults in laravel vapor signed-storage-url在 laravel vapor signed-storage-url 中使用 Axios 默认值
【发布时间】:2019-10-21 20:01:51
【问题描述】:

使用 Tymon jwt 令牌进行身份验证。 Laravel 工作正常。

当为 Laravel vapor 使用上传到 S3 的代码时,我无法获取签名存储 URL 来使用我的 axios 默认值:

axios.defaults.headers.common["Authorization"] = 'Bearer ' + token;

文档中显示的存储方法:

Vapor.store(this.$refs.file.files[0], {
    progress: progress => {
        this.uploadProgress = Math.round(progress * 100);
    }
}).then(response => {
...

它在 npm 包的 index.js 中调用:

async store(file, options = null) {
        // want this to use my default header.
        const response = await axios.post('/vapor/signed-storage-url', {
            'bucket': options.bucket || '',
            'content_type': options.contentType || file.type,
            'expires': options.expires || ''
        });

可能与 npm 模块不在正确范围内有关。

我已经覆盖了 vapor-core signed-storage-url 控制器以使用令牌,并且可以让它与 Postman 一起工作。 它正在调用 Vapor.store,它不会将令牌添加到 axios 调用中,而且我看不到传递标头的方法。

编辑:您无需注册 Vapor 即可使用这些软件包。

composer require laravel/vapor-core

npm install --save-dev laravel-vapor

【问题讨论】:

    标签: laravel vue.js axios laravel-vapor


    【解决方案1】:

    已解决 所以为了完成这项工作,我将异步存储方法复制到我的 vue 组件方法中并从那里调用它。获取默认标头但是...

    这在 S3 中创建了一个 axios 标头问题,通过这样做可以解决:

    async store(file, options = null) {
                const response = await axios.post('/vapor/signed-storage-url', {
                    'bucket': options.bucket || '',
                    'content_type': options.contentType || file.type,
                    'expires': options.expires || ''
                });
    
                if (typeof options.progress === 'undefined') {
                    options.progress = () => {};
                }
    // This is the fix for the headers. Instance just for the S3 PUT
                var instance = axios.create();
                instance.defaults.headers.common = {};
                const s3Response = await instance.put(response.data.url, file, {
                    headers: response.data.headers,
                    onUploadProgress: (progressEvent) => {
                        options.progress(progressEvent.loaded / progressEvent.total);
                    }
                });
    
                response.data.extension = file.name.split('.').pop()
    
                return response.data;
            },
    

    感谢https://github.com/axios/axios/issues/382#issuecomment-254712154

    【讨论】:

    • 为什么这需要在经过身份验证的路由下?
    • @AliGajani 所以我的 S3 存储桶并非对所有人都可用。只有登录用户才能上传图片。
    • 您是否必须定义vapor/signed-storage-url 或者它是预安装的?
    • 签名存储网址是蒸汽核心的一部分。我需要覆盖它,以便我可以将上传设置为公开阅读。 app/overrides/src/Controllers/SignedStorageUrlController.php
    • Vapor-core 已更新以设置可见性。请参阅此提交 github.com/laravel/vapor-core/commit/…
    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2018-10-12
    • 2014-10-17
    • 2017-02-16
    • 2018-05-04
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多