Skip to main content
sharp_quirk16B6
Inspiring
May 10, 2024
Answered

Struggling with downloadObject() to fetch S3 binary file

  • May 10, 2024
  • 2 replies
  • 966 views

I am strugging with downloadObject()  to fetch a PDF file from an S3 bucket. 

s3Svc = getCloudService("iamCreds", "s3config")
bucket = s3Svc.bucket("mybucket", false)
data = bucket.downloadOjbect({ key="myPdfFile.pdf" })

The Adobe documentation states that "key" is the only required parameter but my server, CF2021u13, throws an error and states, "Field type is mandatory and must have a non-null value."

 

The docs fail me again because they don't tell us what the acceptable values are other than "json".  Why is Adobe documentation so sparse?

 

I tried the following:

data = bucket.downloadOjbect({ key="myPdfFile.pdf", type="json" })

This fetches the file content from s3 but then fails with a "JSON parsing failure at character 1" because it tries to parse the binary PDF content as JSON.  Adding useCustomSerializer=true|false doesn't make change anything.

 

I tried changing the value of 'type' to "binary", "object", "struct", "pdf", and "string" but they all give me the error, "xxx is not supported by ColdFusion for serialization or deserialization".

 

All I want to do is download a binary file from s3 and store it in an in-memory variable.  I do not want to write it to disk with downloadToFile().  Surely somebody has figured this out?

 

I haven't been able to find a single example of downloadObject() which downloads a binary file.  Every example I have found is working with a text file.

    This topic has been closed for replies.
    Correct answer Charlie Arehart

    Downloadobject is not for files. It's for objects (such as cf variables, arrays, structs) you upload. See the docs here for more, including examples.

    https://helpx.adobe.com/coldfusion/using/integrate-coldfusion-amazon-s3.html

     

    Downloadtofile is the way to get a file. It will save it to disk by default. You should be able to save it to cf's vfs (ram drive) instead, if saving to file is unacceptable. But downloadobject is not the right tool.

     

    That's how I read things, at least. Perhaps someone else may have more or different thoughts to share. 

    2 replies

    Participant
    November 1, 2024

    Old thread but looking into doing this now. Was able to get the object with type = "xml". Still have other issues with the pdf.

    Community Expert
    November 1, 2024

    Are you using downloadObject or downloadToFile? Files aren't objects. See @Charlie Arehart 's previous replies.

     

    Dave Watts, Eidolon LLC
    Participant
    November 1, 2024

    downloadObject has  object and blob name it will return the pdf. So this is returning the pdf blob object and displaying in browser or you could switch to attachment

    in the cfc that calls azure blob storage, below is stored in a function

    <cfset var blobRequest = {}>
    <cfset blobRequest.blobName = arguments.blobName>
    <cfset blobRequest.type = "xml">
    <cfset var response = variables.container.downloadObject(blobRequest)>


    in the .cfm page calls the above function and displays or downloads pdf without having to use downloadFile
    <cfset blobObject = b.downloadObject(blobName = "#pdfFile#")>
    <cfheader name="content-disposition" value="inline;filename=#listLast(blobObject .blobName,'/')#">
    <cfcontent type="application/pdf" reset="Yes" variable="#toBinary(toBase64(blobObject .object))#">

    Charlie Arehart
    Community Expert
    Charlie ArehartCommunity ExpertCorrect answer
    Community Expert
    May 10, 2024

    Downloadobject is not for files. It's for objects (such as cf variables, arrays, structs) you upload. See the docs here for more, including examples.

    https://helpx.adobe.com/coldfusion/using/integrate-coldfusion-amazon-s3.html

     

    Downloadtofile is the way to get a file. It will save it to disk by default. You should be able to save it to cf's vfs (ram drive) instead, if saving to file is unacceptable. But downloadobject is not the right tool.

     

    That's how I read things, at least. Perhaps someone else may have more or different thoughts to share. 

    /Charlie (troubleshooter, carehart. org)