Firebase 存储:上传后不显示图像文件类型。我如何使用图像?
Firebase storage: Image file types not showing after upload. How do I use the image?
当我使用像 google-cloud 或 multer-gcs 这样的 npm 包将文件上传到 firebase 存储桶(在幕后使用 google 云存储..)文件得到上传。但是,该文件不显示图像类型 (MIME)。另外,如何在我的 node.js 应用程序中使用图像?
这是将图片上传到 firebase 存储桶的代码。使用 multer 处理文件上传..
var path = require('path');
var multer = require('multer');
var gcs = require( 'multer-gcs' );
var storage = gcs({
filename : function( req, file, cb ) {
cb( null, file.fieldname + '-' + Date.now() + path.extname(file.originalname).toLowerCase());
},
bucket : 'p****n-bcXX3.appspot.com', // Required : bucket name to upload
projectId : 'p****n-bcXX3', // Required : Google project ID
keyFilename: './p****n-5cbc725XXXXd.json', // Required : JSON credentials file for Google Cloud Storage
acl : 'publicread' // Optional : Defaults to private
});
在我的路线中:
router.post('/food/create', [authChecker, gcsUpload.single('food_image')], function(req, res, next) {
Controllers.create_food(req, res);
});
当我直接上传文件到存储桶时,类型显示得很清楚。这里有什么问题?
看起来直接multer-gcs doesn't support the ability to add custom metadata to a file. If you use gcloud,你会这样做:
var options = {
destination: 'new-image.png',
metadata: {
contentType: 'image/png'
}
};
bucket.upload('local-image.png', options, function(err, file) {
// Your bucket now contains:
// - "new-image.png" (with the contents of `local-image.png')
// `file` is an instance of a File object that refers to your new file.
});
我会向 multer
的开发人员提出问题,并要求他们支持元数据上传以及 blob 上传,特别是因为 gcloud
已经支持执行此操作的能力。
这是我现在正在做的处理文件上传的事情。
req.file.modifiedname = (Math.random().toString(36)+'00000000000000000').slice(2, 10) + Date.now() + path.extname(req.file.originalname);
var options = {
metadata: {
contentType: req.file.mimetype
},
predefinedAcl : 'publicread'
};
var blob = bucket.file(req.file.modifiedname);
var blobStream = blob.createWriteStream(options);
blobStream.on('error', function (err) {
return next(err);
});
blobStream.on('finish', function() {
publicUrl = format(
'https://storage.googleapis.com/%s/%s',
bucket.name, blob.name
);
req.file.gcsfileurl = publicUrl;
console.log(req.file.gcsfileurl);
next();
});
blobStream.end(req.file.buffer);
当我使用像 google-cloud 或 multer-gcs 这样的 npm 包将文件上传到 firebase 存储桶(在幕后使用 google 云存储..)文件得到上传。但是,该文件不显示图像类型 (MIME)。另外,如何在我的 node.js 应用程序中使用图像?
这是将图片上传到 firebase 存储桶的代码。使用 multer 处理文件上传..
var path = require('path');
var multer = require('multer');
var gcs = require( 'multer-gcs' );
var storage = gcs({
filename : function( req, file, cb ) {
cb( null, file.fieldname + '-' + Date.now() + path.extname(file.originalname).toLowerCase());
},
bucket : 'p****n-bcXX3.appspot.com', // Required : bucket name to upload
projectId : 'p****n-bcXX3', // Required : Google project ID
keyFilename: './p****n-5cbc725XXXXd.json', // Required : JSON credentials file for Google Cloud Storage
acl : 'publicread' // Optional : Defaults to private
});
在我的路线中:
router.post('/food/create', [authChecker, gcsUpload.single('food_image')], function(req, res, next) {
Controllers.create_food(req, res);
});
当我直接上传文件到存储桶时,类型显示得很清楚。这里有什么问题?
看起来直接multer-gcs doesn't support the ability to add custom metadata to a file. If you use gcloud,你会这样做:
var options = {
destination: 'new-image.png',
metadata: {
contentType: 'image/png'
}
};
bucket.upload('local-image.png', options, function(err, file) {
// Your bucket now contains:
// - "new-image.png" (with the contents of `local-image.png')
// `file` is an instance of a File object that refers to your new file.
});
我会向 multer
的开发人员提出问题,并要求他们支持元数据上传以及 blob 上传,特别是因为 gcloud
已经支持执行此操作的能力。
这是我现在正在做的处理文件上传的事情。
req.file.modifiedname = (Math.random().toString(36)+'00000000000000000').slice(2, 10) + Date.now() + path.extname(req.file.originalname);
var options = {
metadata: {
contentType: req.file.mimetype
},
predefinedAcl : 'publicread'
};
var blob = bucket.file(req.file.modifiedname);
var blobStream = blob.createWriteStream(options);
blobStream.on('error', function (err) {
return next(err);
});
blobStream.on('finish', function() {
publicUrl = format(
'https://storage.googleapis.com/%s/%s',
bucket.name, blob.name
);
req.file.gcsfileurl = publicUrl;
console.log(req.file.gcsfileurl);
next();
});
blobStream.end(req.file.buffer);