问题描述
我正在从s3下载一个对象,并从中创建一个读取流对象以处理视频:
I'm downloading an object from s3 and creating a read stream object from it to process a video:
s3.getObject(params).createReadStream()
但是,我需要从中获取元数据,当我仅通过访问其元数据"属性来获取对象时,这是可能的:
However, I need to get the metadata from it which is possible when i just get the object by accessing its 'metadata' property:
s3.getObject()
我该怎么办
-
通过
s3.getObject()
获取对象,从其metadata属性中获取元数据,然后将其转换为读取流?
Get the object via
s3.getObject()
, grab the metadata from its metadata property, and then turn it into a read stream?
var stream = fs.createReadStream(response);
不起作用-输入必须为字符串
var stream = fs.createReadStream(response);
isn't working - input must be a string
-或-
-
通过
s3.getObject().createReadStream()
获取流,并从流中提取元数据吗?
Get the stream via
s3.getObject().createReadStream()
, and extract the metadata from the stream?
据我所知,元数据未在流中传递.
To my knowledge metadata isn't passed within streams.
如果我的假设是错误的,请告诉我,但是我目前仍然满足以下两个需求:
Tell me if my assumptions are wrong, but I am currently stuck with these two needs:
- 获取元数据
- 将其制作为流
推荐答案
您可以通过请求的httpHeaders事件获取元数据.
You can get the metadata via the request's httpHeaders event.
let fs = require('fs')
let aws = require('aws-sdk')
let s3 = new aws.S3()
let request = s3.getObject({
Bucket: 'my-bucket',
Key: 'my-key'
})
let stream
request.on('httpHeaders', (statusCode, httpHeaders) => {
// object metadata is represented by any header in httpHeaders starting with 'x-amz-meta-'
// you can use the stream object that this point
stream.pipe(fs.createWriteStream('./somepath'))
stream.on('end', () => {
console.log('were done')
})
})
stream = request.createReadStream()
或者,您也可以调用s3.headObject
来获取元数据而不下载对象,然后使用s3.getObject
Alternatively you can also call s3.headObject
to get the metadata without downloading the object and then download the object using s3.getObject
这篇关于获取s3对象元数据,然后创建流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!