Google does not allow accessing its Cloud Storage that way. Ask Google support or search on the stack overflow such as this one to learn how to download or read to a stream.
https://stackoverflow.com/questions/71097537/how-to-download-file-from-google-cloud-storage
After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:
- async function downloadIntoMemory() {
- // Downloads the file into a buffer in memory.
- const contents = await storage.bucket(bucketName).file(destFileName).download();
-
- console.log(
- `Contents have been downloaded`
- );
-
- //console.log(Buffer.from(contents.toString()).toString());
- create_greeting(contents.toString('binary'));
- }
-
- async function create_greeting(data) {
- try {
- let FormData = require('form-data');
- formData = new FormData();
- let body = {
- type: "Introductory",
- answeringRule: {id: "3839201036"}
- }
-
- formData.append('json', new Buffer.from(JSON.stringify(body)), {
- filename: 'request.json',
- contentType: 'application/json'
- });
-
- formData.append('binary', fs.createReadStream(data));
- let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
- let jsonObj = await resp.json()
- .then(() => console.log(JSON.stringify(jsonObj)));
- } catch(e) {
- console.log(e.message)
- }
- }
-
- downloadIntoMemory().catch(console.error);
When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes". How can I change the data buffer from the Cloud Storage download into an acceptable format?
After reviewing the article and the Google Cloud Storage documentation, I can't get the FormData to accept the data downloaded into a buffer. I've tried the raw buffer data, converting to string, converting to binary, and I can't get it to accept the data format. For the purposes of this app, downloading to a file is not an option. Here is my current code:
async function downloadIntoMemory() {
// Downloads the file into a buffer in memory.
const contents = await storage.bucket(bucketName).file(destFileName).download();
console.log(
`Contents have been downloaded`
);
//console.log(Buffer.from(contents.toString()).toString());
create_greeting(contents.toString('binary'));
}
async function create_greeting(data) {
try {
let FormData = require('form-data');
formData = new FormData();
let body = {
type: "Introductory",
answeringRule: {id: "3839201036"}
}
formData.append('json', new Buffer.from(JSON.stringify(body)), {
filename: 'request.json',
contentType: 'application/json'
});
formData.append('binary', data);
let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
let jsonObj = await resp.json()
.then(() => console.log(JSON.stringify(jsonObj)));
} catch(e) {
console.log(e.message)
}
}
When I run this code, I get the error: "Invalid attachment media type". When I run the code but with fs.createReadStream(data) instead, I get this error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3x04x00x00x00x00x00x1CTSSEx00x00x00x00x00x03Googlex00x00x00x00x00x00x00x00x00x00x00Dx00x00x00...". How can I get it to accept the data type without creating a file?
After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:
- async function downloadIntoMemory() {
- // Downloads the file into a buffer in memory.
- const contents = await storage.bucket(bucketName).file(destFileName).download();
-
- console.log(
- `Contents have been downloaded`
- );
-
- //console.log(Buffer.from(contents.toString()).toString());
- create_greeting(contents.toString('binary'));
- }
-
- async function create_greeting(data) {
- try {
- let FormData = require('form-data');
- formData = new FormData();
- let body = {
- type: "Introductory",
- answeringRule: {id: "3839201036"}
- }
-
- formData.append('json', new Buffer.from(JSON.stringify(body)), {
- filename: 'request.json',
- contentType: 'application/json'
- });
-
- formData.append('binary', fs.createReadStream(data));
- let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
- let jsonObj = await resp.json()
- .then(() => console.log(JSON.stringify(jsonObj)));
- } catch(e) {
- console.log(e.message)
- }
- }
-
- downloadIntoMemory().catch(console.error);
When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3x04x00x00x00x00x00x1CTSSEx00x00x00x00x00x03Googlex00x00x00x00x00x00x00x00x00x00x00Dx00x00x00...". How can I change the data buffer from the Cloud Storage download into an acceptable format?