通过驱动器 api 上传时 Base64 字符串 img 不可见 [英] Base64 string img not viewable when uploaded via drive api
问题描述
我已通过 node express 中的 API 将 base64 img 字符串上传到 Google Drive.上传 img 后,在云端硬盘中无法查看.我不确定如何解决此格式问题.我知道我可以先在本地保存 img,然后上传保存的 img 文件,但我希望有一种更简单的方法.
I have uploaded a base64 img string to Google Drive via API in node express. After uploading the img, it is not viewable in Drive. I'm not sure on how to resolve this formatting issue. I know I could potentially save the img locally first, then upload the saved img file but I was hoping there is a simpler way.
我的代码:
const uploadImg = async (folderId,img)=>{
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
const scopes = [
'https://www.googleapis.com/auth/drive'
];
const auth = new google.auth.JWT(
demoApiCreds.client_email, null,
demoApiCreds.private_key, scopes
);
const drive = google.drive({ version: 'v3', auth });
const fileMetadata = {
'name': 'Client_Design_ScreenShotTest',
'mimeType':'image/jpeg',
'parents':[folderId]
};
const uploadImg = img.split(/,(.+)/)[1];
const media = {
body: uploadImg
}
let res = await drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id',
});
console.log('the response is',res);
console.log('the data is ',res.data);
return res.data;
}
文件以jpg格式存储在驱动器中,但img为空白且之后img被点击谷歌驱动器抱怨该文件不能读.下载后img还是空白.
The file is stored in drive, as a jpg, but the img is blank and after the img is clicked google drive complains that the file cannot be read. The img is still blank after downloading.
base 64 img 字符串是
The base 64 img string is
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhAAAADqCAYAAADzlnzfAAAAAXNSR0I...
我在上传之前删除了 data:image/png;base64,正如其他线程中所建议的那样.不管有没有这个前缀,它都会失败.
I remove data:image/png;base64 before uploading as has been suggested in other threads. It fails with or without this prefix.
推荐答案
- 您想使用带有 node.js 的 googleapis 将图像上传到 Google 云端硬盘.
img
的图片是base64数据.- 您已经能够使用 Drive API 将文件上传和下载到 Google Drive.
- You want to upload an image to Google Drive using googleapis with node.js.
- The image of
img
is the base64 data. - You have already been able to upload and download files to Google Drive using Drive API.
- 很遗憾,使用googleapis上传base64数据时,base64数据没有被解码,数据是作为文本数据上传的.因此,当您看到上传的文件时,您无法将其视为图像.如果
Content-Transfer-Encoding: base64
可以添加到请求体中base64数据的头部,则base64数据被转换成图片上传.但是使用googleapis的时候,现阶段是无法实现的. - Unfortunately, when the base64 data is uploaded using googleapis, the base64 data is not decoded and the data is upload as the text data. So when you see the uploaded file, you cannot see it as the image. If
Content-Transfer-Encoding: base64
can be added to the header of the base64 data in the request body, the base64 data is converted and uploaded as an image. But when googleapis is used, in the current stage, it cannot be achieved.
如果我的理解是正确的,那么这个答案呢?请认为这只是几个答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
为了将图片编码的base64数据作为图片上传到Google Drive,下面的修改如何?
In order to upload the base64 data encoded from an image as an image to Google Drive, how about the following modification?
本次修改将base64图片转换为流类型,上传.请按如下方式修改您的脚本.
In this modification, the base64 image is converted to the stream type, and uploaded. Please modify your script as follows.
const uploadImg = img.split(/,(.+)/)[1];
const media = {
body: uploadImg
}
到:
const stream = require("stream"); // Added
const uploadImg = img.split(/,(.+)/)[1];
const buf = new Buffer.from(uploadImg, "base64"); // Added
const bs = new stream.PassThrough(); // Added
bs.end(buf); // Added
const media = {
body: bs // Modified
};
注意:
- 即使
'mimeType':'image/jpeg'
在fileMetadata
中使用,图像文件也会以image/png
的形式上传.但是例如,如果'mimeType':'application/pdf'
在fileMetadata
中使用,则图像文件将作为application/pdf
上传.请注意这一点.所以我还建议修改为 10100111001 的回答中提到的'mimeType':'image/png'
一个>. - 在googleapis@43.0.0"中,
resource: fileMetadata
和requestBody: fileMetadata
两种模式都有效. - Even if
'mimeType':'image/jpeg'
is used atfileMetadata
, the image file is uploaded asimage/png
. But for example, if'mimeType':'application/pdf'
is used atfileMetadata
, the image file is uploaded asapplication/pdf
. Please be careful this. So I also recommend to modify to'mimeType':'image/png'
as mentioned by 10100111001's answer. - At "googleapis@43.0.0", both patterns of
resource: fileMetadata
andrequestBody: fileMetadata
work.
Note:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
这篇关于通过驱动器 api 上传时 Base64 字符串 img 不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!