我想将图像从 url 上传到我的 Amazon S3 buckett。
我可以使用以下代码上传,但结果是网址,而不是图片...
在我的 Angular5 控制器中:
import { AwsService } from '../../services/aws.service';
...
@Injectable()
export class MyClass {
constructor(
private aws: AwsService,
) {}
sendimg() {
var imgurl = 'https://www.google.be/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
const allparams = {
Body: imgurl,
Key: place_id + 'logo.png',
ContentType: 'png',
ACL: 'public-read'
};
this.aws.s3upload(allparams, 'mybuckett');
}
}
在 aws.service.ts 中:
import { S3 } from 'aws-sdk';
import { Config } from '../app/config';
...
@Injectable()
export class AwsService {
s3upload(allparams, bucket) {
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: Config.AWS.accessKeyId,
secretAccessKey: Config.AWS.secretAccessKey,
region: Config.AWS.region
});
var s3 = new AWS.S3({
params: { Bucket: bucket}
});
s3.upload(allparams, function (err, data) {
if (err) {
alert(err);
}
});
}
}
是否可以直接执行此操作?还是我应该先从 URL 下载文件,然后将其上传到我的 S3 存储桶?
我怎样才能做到这一点 ?