Aka*_*han 8 node.js google-cloud-storage
我正在从网址读取图像并进行处理.我需要将此数据上传到云存储中的文件,目前我正在将数据写入文件并上传此文件然后删除此文件.有没有办法可以将数据直接上传到云存储?
static async uploadDataToCloudStorage(rc : RunContextServer, bucket : string, path : string, data : any, mimeVal : string | false) : Promise<string> {
if(!mimeVal) return ''
const extension = mime.extension(mimeVal),
filename = await this.getFileName(rc, bucket, extension, path),
modPath = (path) ? (path + '/') : '',
res = await fs.writeFileSync(`/tmp/${filename}.${extension}`, data, 'binary'),
fileUrl = await this.upload(rc, bucket,
`/tmp/${filename}.${extension}`,
`${modPath}${filename}.${extension}`)
await fs.unlinkSync(`/tmp/${filename}.${extension}`)
return fileUrl
}
static async upload(rc : RunContextServer, bucketName: string, filePath : string, destination : string) : Promise<string> {
const bucket : any = cloudStorage.bucket(bucketName),
data : any = await bucket.upload(filePath, {destination})
return data[0].metadata.name
}
Run Code Online (Sandbox Code Playgroud)
是的,可以从URL检索图像,对图像执行编辑,然后使用nodejs将其上传到Google Cloud Storage(或Firebase存储),而无需在本地保存文件.
这是基于Akash的答案,其中包含一个对我有用的功能,包括图像处理步骤.
如果您是使用firebase存储的firebase用户,则仍必须使用此库.用于存储的firebase Web实现在节点中不起作用.如果您在firebase中创建了存储,则仍可通过Google云端存储控制台访问此存储.他们是一样的东西.
const axios = require('axios');
const sharp = require('sharp');
const { Storage } = require('@google-cloud/storage');
const processImage = (imageUrl) => {
return new Promise((resolve, reject) => {
// Your Google Cloud Platform project ID
const projectId = '<project-id>';
// Creates a client
const storage = new Storage({
projectId: projectId,
});
// Configure axios to receive a response type of stream, and get a readableStream of the image from the specified URL
axios({
method:'get',
url: imageUrl,
responseType:'stream'
})
.then((response) => {
// Create the image manipulation function
var transformer = sharp()
.resize(300)
.jpeg();
gcFile = storage.bucket('<bucket-path>').file('my-file.jpg')
// Pipe the axios response data through the image transformer and to Google Cloud
response.data
.pipe(transformer)
.pipe(gcFile.createWriteStream({
resumable : false,
validation : false,
contentType: "auto",
metadata : {
'Cache-Control': 'public, max-age=31536000'}
}))
.on('error', (error) => {
reject(error)
})
.on('finish', () => {
resolve(true)
});
})
.catch(err => {
reject("Image transfer error. ", err);
});
})
}
processImage("<url-to-image>")
.then(res => {
console.log("Complete.", res);
})
.catch(err => {
console.log("Error", err);
});
Run Code Online (Sandbox Code Playgroud)
使用节点流可以上传数据而无需写入文件。
const stream = require('stream'),
dataStream = new stream.PassThrough(),
gcFile = cloudStorage.bucket(bucketName).file(fileName)
dataStream.push('content-to-upload')
dataStream.push(null)
await new Promise((resolve, reject) => {
dataStream.pipe(gcFile.createWriteStream({
resumable : false,
validation : false,
metadata : {'Cache-Control': 'public, max-age=31536000'}
}))
.on('error', (error : Error) => {
reject(error)
})
.on('finish', () => {
resolve(true)
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3333 次 |
| 最近记录: |