2016-11-03 3 views
0

My Angular 1アプリケーションは、ファイルをS3に保存し、さまざまな種類のファイルを許可します。ノード内のS3からダウンロードして新しいウィンドウで開く

私は次のコードを使用するオブジェクト取得する場合:

export function show(req, res) { 
    const s3 = new aws.S3(); 
     const s3Params = { 
     Bucket: S3_BUCKET, 
     Key: req.query.key + '' 
     }; 
     res.attachment(req.query.key + ''); 
     var fileStream = s3.getObject(s3Params).createReadStream(); 
     fileStream.pipe(res); 
} 

を私は(ちょうどAWSコンソール上のような)新しいウィンドウでクライアントに受信したファイルを開きたいが、私はできませんそれについてどうやって行くのかを理解する。

クライアント側で例えば全く動作しません:私は本当にデータの概念が作品をストリームする方法を理解していない

.then(
    (data) => { 
     var file = new Blob([data], {type: 'application/pdf'}); 
     var fileURL = URL.createObjectURL(file); 
     window.open(fileURL); 
    } 
) 

答えて

0

私がしなければならなかったことは、これを行うためのリソースのsignedUrlを取得することでした。

export function show(req, res) { 
    const s3 = new aws.S3(); 
    const s3Params = { 
    Bucket: S3_BUCKET, 
    Key: req.query.key + '' 
    }; 
    s3.getSignedUrl('getObject', s3Params, (err, data) => { 
    if (err) { 
     console.log(err); 
     return res.end(); 
    } 
    const returnData = { 
     signedRequest: data, 
    }; 
    res.write(JSON.stringify(returnData)); 
    res.end(); 
    }); 
} 

と、クライアント上の私がしなければならないすべてが新しいタブでリンクを開いている:

openDoc(doc) { 
this.$http() 
    .then(
    (data) => { 
     this.$window.open(data.data.signedRequest, '_blank') 
    } 
) 
    .catch(
    (err) => { 
     this.Notification.error('failed to download attachment'); 
    } 
) 
} 
1

pdfをダウンロードする必要がない場合は、s3から直接開くことができます。

s3client.getResourceUrl("your-bucket", "some-path/some-key.jpg"); 

これはあなたにURLを返します。 だからあなたのようなコードが必要です。ごめんなさい

export function show(req, res) { 
 
    this.s3client = new aws.S3({ 
 
    accessKeyId: options.accessKeyId, 
 
    secretAccessKey: options.secretAccessKey, 
 
    region: options.region 
 
    }) 
 
    let resourceUrl = s3client.getResourceUrl(S3_BUCKET, req.query.key + ''); 
 
    window.open(resourceUrl, '_blank'); 
 
}

を、今それをテストが、試すことができません。うまくいくはずです。

+0

getResourseUrlは、ノードAWSライブラリにあるように表示されません。 – Mika

+0

私はチェックしました - そうです、そのような方法はありません。 – Freakachoo

+0

HTTP(S):// .s3.amazonaws.com/ HTTP(S)://s3.amazonaws.com/ / Freakachoo

関連する問題