如何在Amazon S3中将其公开

时间:2019-02-08 06:24:05

标签: node.js amazon-s3

上传后如何公开照片?我的配置s3.config.js

import { S3 } from 'aws-sdk';
import config from '../config';

const s3Client = new S3({
    accessKeyId: config.env.aws_access_key,
    secretAccessKey: config.env.aws_secret_access_key,
    region: config.env.region
});

const uploadParams = {
    ACL: 'public-read',
    Bucket: config.env.bucket,
    Key: '',
    Body: null
};

export default {
    s3Client: s3Client,
    uploadParams: uploadParams,
};

接下来,我尝试上传图像,但是出现错误Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client。为什么会发生这种情况,为什么图像仍然被破坏?

import s3 from '../config/s3.config.js';
import upload from'../config/multer.config.js';

const s3Client = s3.s3Client;
const params = s3.uploadParams;

router.post('/add',(req, res) => {
    params.Key = 'avatar/' + Date.now();
    params.Body = req.file.buffer;
    s3Client.upload(params, (err, data) => {
        if (err) {
            return res.status(500).json({
                error: err
            });
        }
        console.log('imageUrl ' + data.Location);
    });
});

帮助解决此问题

0 个答案:

没有答案