Help to Upload Video from media.captureVideo to Parse

I’ve tried a bunch of things to upload a video to parse and I’m really close. I have the following information that would help us figure this out: Parse REST API, plus a file that uses the Multiform Data ( http://developer.coronalabs.com/code/how-upload-image-server-multipartform-data). First, I have the following code which ends up giving me a ‘Bad Gateway - Error 502 - The server returned an invalid or incomplete response’ in the Upload response. 

local function onComplete(event)

    if event.completed then

        local videoURL = event.url

        local sourcePath = string.sub(videoURL,8)       – prints out path. ex… “/storage/emulated/0/DCIM/Camera/20140706_175957.mp4”

        local videoName = string.sub(videoURL, 40)    – prints out video title and extension. ex…“20140706_175957.mp4”

        local videoFile = string.sub(videoURL, 40, -5)   – prints out video title. ex… “20140706_175957”

        multipart:addHeader(“X-Parse-Application-Id”, myData.appId)

        multipart:addHeader(“X-Parse-REST-API-Key”, myData.apiKey)

        – multipart:addHeader(“Content-Type”, “video/mp4”) – Maybe Needed

        multipart:addFile(videoFile, sourcePath, “video/mp4”, videoName)

        local params = {}

        params.body = multipart:getBody()             – Must call getBody() first!

        params.headers = multipart:getHeaders()   – Headers not valid until getBody() is called.

        local function onFileUploaded( event )

            if not event.error then

                if event.phase == “ended” then

                    local resulted = event.response   – Currently Get Bad Gateway 502 Error Code

                    local statused = event.status       – Prints out 502

                end

            else

        

            end

        end

        network.request( “https://api.parse.com/1/files/”…videoName, “POST”, onFileUploaded, params)

    else

    end

end

if media.hasSource( media.Camera ) then

    media.captureVideo( { listener = onComplete, preferredMaxDuration = 3, } )

else

    native.showAlert( “Sorry”, “This device does not have a camera.”, { “OK” } )

end


Uploading Files

To upload a file to Parse, send a POST request to the files URL, postfixed with the name of the file. The request must contain the Content-Type header associated with the file. Keep in mind that files are limited to 10 megabytes. Here’s a simple example that’ll create a file named hello.txt containing a string:

curl -X POST \

  -H “X-Parse-Application-Id: L39R3jVAaGSvjmLtqntxbejApgJjhe15afK5Z5bI” \

  -H “X-Parse-REST-API-Key: QwOBH0wwKEYX2edHIgdYhbPUD6nk9xDG0otFQDHy” \

  -H “Content-Type: text/plain” \

  -d ‘Hello, World!’ \

  https://api.parse.com/1/files/hello.txt

Show examples for:                 python                 curl              Use keys for:                 Gotcha You R It            

When the file upload is successful, the HTTP response is a 201 Created and the Location header which contains the URL for the file:

Status: 201 Created

Location: http://files.parsetfss.com/bc9f32df-2957-4bb1-93c9-ec47d9870a05/tfss-db295fb2-8a8b-49f3-aad3-dd911142f64f-hello.txt

The response body is a JSON object containing the name of the file, which is the original file name prefixed with a unique identifier in order to prevent name collisions. This means you can save files with the same name, and the files will not overwrite one another.

{

  “url”: “http://files.parsetfss.com/bc9f32df-2957-4bb1-93c9-ec47d9870a05/tfss-db295fb2-8a8b-49f3-aad3-dd911142f64f-hello.txt”,

  “name”: “db295fb2-8a8b-49f3-aad3-dd911142f64f-hello.txt”

}

To upload an image, the syntax is a little bit different. Here’s an example that will upload the image myPicture.jpg from the current directory.

curl -X POST \

  -H “X-Parse-Application-Id: L39R3jVAaGSvjmLtqntxbejApgJjhe15afK5Z5bI” \

  -H “X-Parse-REST-API-Key: QwOBH0wwKEYX2edHIgdYhbPUD6nk9xDG0otFQDHy” \

  -H “Content-Type: image/jpeg” \

  --data-binary ‘@myPicture.jpg’ \

  https://api.parse.com/1/files/pic.jpg

Show examples for:                 python                 curl              Use keys for:                 Gotcha You R It            

Hi Scott,

Thanks for posting your code. I hope you have yours in working order. 

I am trying to use the plugin: mod_parse: https://bitbucket.org/develephant/mod_parse/wiki/Home

which is really nice to use and I highly recommend it for interacting with Parse.

The issue I am having is that when I take a video, I cannot seem to access it properly because as seen here: http://coronalabs.com/blog/2013/11/05/tutorial-captureselect-photo-and-video/ 

IMPORTANT: Videos are captured/selected in an area outside of your app’s sandbox and technically are accessed via a URL. Because of this, you need to specify media.RemoteSource in the playback API when playing the video.

Do you know of any good ways in which I can access this? 

I tried using:

local videoURL = event.url

local videoDirectory  = string.sub(videoURL, 8, -20) 

to grab the directory of the video, which is needed for the upload function in mod_parse, but no luck.

Thanks!

-Zeb

Hi Scott,

Thanks for posting your code. I hope you have yours in working order. 

I am trying to use the plugin: mod_parse: https://bitbucket.org/develephant/mod_parse/wiki/Home

which is really nice to use and I highly recommend it for interacting with Parse.

The issue I am having is that when I take a video, I cannot seem to access it properly because as seen here: http://coronalabs.com/blog/2013/11/05/tutorial-captureselect-photo-and-video/ 

IMPORTANT: Videos are captured/selected in an area outside of your app’s sandbox and technically are accessed via a URL. Because of this, you need to specify media.RemoteSource in the playback API when playing the video.

Do you know of any good ways in which I can access this? 

I tried using:

local videoURL = event.url

local videoDirectory  = string.sub(videoURL, 8, -20) 

to grab the directory of the video, which is needed for the upload function in mod_parse, but no luck.

Thanks!

-Zeb

Hi Zeb,

Do you found the solution to this problem?

I’m trying to upload some images to parse and have the same problem.

Thanks in advance,

Josep Alemany

Hi Zeb,

Do you found the solution to this problem?

I’m trying to upload some images to parse and have the same problem.

Thanks in advance,

Josep Alemany