将 S3 字节数组转换为 base64

在谷歌图表中,要真正拥有不同颜色的条形“组”,

您需要使用单独的列或系列...


var data = google.visualization.arrayToDataTable([

  ['Range', 'Series 0', 'Series 1'],

  ['Today', 1000, 1170],

]);

但是,在上面的示例中,您会注意到只有一行,

所以没有昨天。


为了重现所需的图表,

您首先需要使用样式角色,以便分别为每个条形着色。


您还需要插入空白行以制作“组”...


var data = google.visualization.arrayToDataTable([

  ['Range', 'Value', {role: 'style', type: 'string'}],

  ['Today', 1000, '#3366cc'],

  ['Yesterday', 1170, '#dc3912'],

  ['', null, null],

  ['this week', 7000, '#3366cc'],

  ['last week', 9170, '#dc3912'],

]);

请参阅以下工作片段...


google.charts.load('current', {

  packages: ['corechart']

}).then(function () {

  var data = google.visualization.arrayToDataTable([

    ['Range', 'Value', {role: 'style', type: 'string'}],

    ['Today', 1000, '#3366cc'],

    ['Yesterday', 1170, '#dc3912'],

    ['', null, null],

    ['this week', 7000, '#3366cc'],

    ['last week', 9170, '#dc3912'],

  ]);


  var options = {

    legend: {position: 'none'},

    title: 'Vehicle Operating Rate'

  };


  var chart = new google.visualization.BarChart(document.getElementById('chart'));

  chart.draw(data, options);

});

<script src="https://www.gstatic.com/charts/loader.js"></script>

<div id="chart"></div>在我的 NodeJS 服务器上,我下载了一个需要嵌入到电子邮件中的图像。我的存储桶不是公开的,因此仅使用该链接是行不通的,因为这不是我想要的针对此问题或项目要求的解决方案。


我为此使用了 HTML 电子邮件,内容如下:


<p>Embedded image: <img src="data:image/jpeg;charset=utf-8;base64,{{url}}" /></p>

所以我从S3下载


s3.getObject(

                          { Bucket: "mybucket", Key: "mykey" },

                          function (error, data) {

                            if (error != null) {

                              console.log("Errror" + error)

                            } else {

                              console.log("Loaded " + data.ContentLength + " bytes")

然后我试图将 data.body 转换为 UTF-8 base 64


我想像


"data:image/png;base64," + new String(encoder.encode(data.body), "UTF-8")

但这似乎不起作用,我正在努力定义编码器以实现此目的。


江户川乱折腾
浏览 288回答 2
2回答

拉莫斯之舞

确保您在data.Body. 我认为data.Body已经是一个缓冲区,您可以按如下方式构造 src URL:&nbsp; // Convert Body from a Buffer to a String&nbsp; &nbsp; let base64String= data.Body.toString('base64');&nbsp; &nbsp; let src = "data:image/jpeg;base64,"+base64String;

MMMHUHU

如果您以二进制格式存储 s3 存储桶对象,则可以接收它并将其转换为 base64:const blobToBase64 = (blob) => {&nbsp; const reader = new FileReader()&nbsp; reader.readAsDataURL(blob)&nbsp; return new Promise((rs, rj) => {&nbsp; &nbsp; reader.onloadend = () => {&nbsp; &nbsp; &nbsp; rs(reader.result)&nbsp; &nbsp; }&nbsp; &nbsp; reader.onerror = rj&nbsp; })}function useImage({s3Link}) {&nbsp; const [src, setSrc] = React.useState(null)&nbsp; React.useEffect(() => {&nbsp; &nbsp; async function query({link}) {&nbsp; &nbsp; &nbsp; //link is link to s3 bucket URL link e.g&nbsp; &nbsp; &nbsp; // const link = s3.getSignedUrl('getObject', {&nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;Bucket: bucketnamw,&nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;Key: key,&nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;Expires: 30,&nbsp; &nbsp; &nbsp; // })&nbsp; &nbsp; &nbsp; const r = await fetch(link)&nbsp; &nbsp; &nbsp; const blob = await r.blob()&nbsp; &nbsp; &nbsp; const base64 = await blobToBase64(blob)&nbsp; &nbsp; &nbsp; console.log(`base64!`, base64)&nbsp; &nbsp; &nbsp; setSrc(base64)&nbsp; &nbsp; }&nbsp; &nbsp; if (s3Link) {&nbsp; &nbsp; &nbsp; query({link: s3Link})&nbsp; &nbsp; }&nbsp; }, [s3Link, setSrc])&nbsp; return [{src}]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript