3

I have a problem. In my project I get a text and send this text to remote API in .txt file. Now the program does this: getting a text, saving a text in a .txt file in filesystem, uploading a .txt file to remote API. Unfortunately, remote API accepts only files, I can't send plain text in request.

//get the wallPost with the field text
fs.writeFileSync(`./tmp/${wallPostId}.txt`, wallPost.text)

remoteAPI.uploadFileFromStorage(
  `${wallPostPath}/${wallPostId}.txt`,
  `./tmp/${wallPostId}.txt`
)

UPD: In function uploadFileFromStorage, I made a PUT request to remote api with writing a file. Remote API is API of cloud storage which can save only files.

const uploadFileFromStorage = (path, filePath) =>{
let pathEncoded = encodeURIComponent(path)
const requestUrl = `https://cloud-api.yandex.net/v1/disk/resources/upload?&path=%2F${pathEncoded}`
const options = {
  headers: headers
}

axios.get(requestUrl, options)

.then((response) => {
  const uploadUrl = response.data.href
  const headersUpload = {
    'Content-Type': 'text/plain',
    'Accept': 'application/json',
    'Authorization': `${auth_type} ${access_token}`
  }
  const uploadOptions = {
    headers: headersUpload
  }
  axios.put(
    uploadUrl,
    fs.createReadStream(filePath),
    uploadOptions
  ).then(response =>
    console.log('uploadingFile: data  '+response.status+" "+response.statusText)
  ).catch((error) =>
    console.log('error uploadFileFromStorage '+ +error.status+" "+error.statusText)
  )
})

But i guess in the future such a process will be slow. I want to create and upload a .txt file in RAM memory (without writing on drive). Thanks for your time.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Myxomor
  • 33
  • 4
  • Okay, _which_ remote API? Because the most obvious solution is "stop using `uploadFileFromStorage`, it's clearly for files, use the API call for plain data", but you've not given enough information for people with knowledge of the service you're using to help you. – Mike 'Pomax' Kamermans Jan 30 '21 at 19:33
  • Oh, sorry, I forgot about it. Remote API accepts only files, it's api of cloud storage – Myxomor Jan 30 '21 at 19:43
  • In APIs, I can't figure out what the difference is between 'accepting plain text' vs 'accepting a plain text file'. – Evert Jan 30 '21 at 19:49
  • @Evert , I added info about api – Myxomor Jan 30 '21 at 19:59
  • In terms of the API end point accepting the data there is almost certainly no difference whatsoever, but for the API _library_ that you use in your own code, the difference is in what "their code" does. So in this case that's between "having a buffer" vs. "there needs to exist a file on disk". – Mike 'Pomax' Kamermans Jan 30 '21 at 19:59
  • @Mike'Pomax'Kamermans unfortunatly, this remote API hasn't their library. They represend only REST API for working with web. please, check my update the code. – Myxomor Jan 30 '21 at 20:24
  • Thank you for updating your post: that's the Yandex _Disk_ API so yeah it makes sense they want files, not just strings: that's what it's for, it explicitly stores files on a remote disk =) – Mike 'Pomax' Kamermans Jan 31 '21 at 18:25

1 Answers1

1

You're using the Yandex Disk API, which expects files because that's what it's for: it explicitly stores files on a remote disk.

So, if you look at that code, the part that supplies the file content is supplied via fs.createReadStream(filePath), which is a Stream. The function doesn't care what builds that stream, it just cares that it is a stream, so build your own from your in-memory data:

const { Readable } = require("stream");

...

const streamContent = [wallPost.text];
const pretendFileStream = Readable.from(streamContent);

...

axios.put(
  uploadUrl,
  pretendFileStream,
  uploadOptions
).then(response =>
  console.log('uploadingFile: data  '+response.status+" "+response.statusText)
)

Although I don't see anything in your code that tells the Yandex Disk API what the filename is supposed to be, but I'm sure that's just because you edited the post for brevity.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Thank you very much! I will write when I get results of testing. And the filename is implicitly passed in the PATH parameter in the first request and is the same as the temporary filename in the tmp folder) – Myxomor Jan 31 '21 at 20:33