使用签名URL在s3中保存jpg图像

时间:2018-03-20 02:14:10

标签: amazon-s3 base64 aws-lambda angular-http-interceptors pre-signed-url

我正在使用AWS Lambda返回的签名URL将带有Angular HTTPClient的jpg图像上传到S3存储桶。我可以看到image.jpg文件但是当我打开它时,它说它是无效的格式。请帮忙!

我希望我需要这些HTTP请求标头:

'Content-Type': 'image/jpeg'
'Content-Encoding': 'base64'

我尝试使用和不使用

"data:image/jpeg;base64," 

后跟基于64的编码数据作为字符串。

我的lambda是:



var AWS = require('aws-sdk');

var s3 = new AWS.S3({
  signatureVersion: 'v4',
});

exports.handler = (event, context, callback) => {
  const url = s3.getSignedUrl('putObject', {
    Bucket: 'landlord-bucket',
    Key: 'image' + '.jpg',
    Expires: 20,
    ContentEncoding: 'base64',
    ContentType: 'image/jpeg',
  });

  callback(null, url);
};




我使用以下方式上传:



@Effect() postToS3$: Observable<Action> = this.actions$
    .ofType(PropertyActions.UPLOAD_FILE_PUTS3)
    .switchMap((action: PropertyActions.UploadFilePutS3) => {
        return this.httpClient.put(action.payload, "data:image/jpeg;base64," + this.filesToUpload)
            .pipe(map(res => {
                    return new PropertyActions.OpenAllProperties(res);},
                err => {
                    console.log("Error occured in get signed url");
                }))
        })
&#13;
&#13;
&#13;

this.filesToUpload是base64编码的jpg的字符串。这是正确编码的,因为我可以在任何解码器站点看到图像。

HTTP拦截器是:

&#13;
&#13;
@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(private authService: AuthService) {}
 
    intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        return this.authService.getAuthenticatedUser().getSession((err, session) => {
            if (err) {
                console.log("Error getSession")
                return next.handle(req);
            }

            let authReq = null;
            
            if (req.url.indexOf('X-Amz-Algorithm') === -1) {
           
                authReq = req.clone({
                    headers: req.headers.set('Authorization', session.getIdToken().getJwtToken())
                });
            } else {
                authReq = req.clone({
                    setHeaders: {'Content-Type': 'image/jpeg', 'Content-Encoding': 'base64'}
                });
            }
            
            return next.handle(authReq);
        })
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案