UploadGig
API
Please attention: This document is under development.

Get Upload Action:

To upload files in UploadGig, you need permission and action URL. this API will help you to give this information.

  • API address: https://uploadgig.com/api/get_upload_action
  • Method: post
  • Request:
    • user : account's username
    • pass : account's password
  • Response (json):
    • code (int) response code
    • result body of response
Note: If code parameter is equal to 1, it means this process was successful, and result parameter contains an array that has all of the upload information. In all other cases, the result contains an error string.

Example:

$username = '[email protected]';
$password = '123';
$ch = curl_init('https://uploadgig.com/api/get_upload_action');
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(
    'user' => $username,
    'pass' => $password
));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo $response;
import requests

username = '[email protected]'
password = '123'
action_api_url = 'https://uploadgig.com/api/get_upload_action'

response = requests.post(action_api_url, data={
    'user': username,
    'pass': password
}, allow_redirects=True, timeout=5)
print(response.text)

Output (successful):

{
  code: "1",
  result: {
    action: "http://xxx.xxx.xxx.xxx:81/upload?identity=yyyyyyyyyyyyyyyy",
    cmbs: 10240
  }
}
Note: cmbs is the max of allowed upload size (in megabyte) to this action.
Note: The action URL will be valid for 60 seconds, after this time it will be removed.


Output (failed):

{
  code: "0",
  result: "Email or password was incorrect!"
}

Upload File:

After you get the upload permission in the previous step, you must send the file as a binary content(raw body upload), to avoid memory issues when uploading big files, we support chunked transfer encoding too.

  • Upload address: The action value in the previous step.
  • Request:
    • It is shown in the following sample code.
  • Response (json):
    • 0 :
      • ok (boolean) the upload state
      • message (string) indicates a short explanation of upload state
      • url (string) only on successful upload, it's the file's URL
      • file_name (string) only on successful upload, it's the name of file
      • size (int) only on successful upload, it's the size of file in byte
      • id (string) only on successful upload, identity of the uploaded file
Note: Please check your file size with the cmbs (mb) parameter from previous step.

Example:

$fp = '/path/to/file';

// check file size limit in megabyte
if ( (filesize($fp)/1024/1024) > $cmbs ) {
    exit('File too large');
}

$stream = fopen($fp, 'rb');

$ch = curl_init($action);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 60);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 32768);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Transfer-Encoding: chunked', 'Slug: ' . basename($fp)
]);
curl_setopt($ch, CURLOPT_READFUNCTION, function($_, $__, $chunk_size) use (&$stream) {
    return fread($stream, $chunk_size);
});

$response = json_decode(curl_exec($ch), true)[0];
fclose($stream);
curl_close($ch);

if ($response['ok']) {// upload successful
    echo $response['url'];
} else { // upload failed
    echo $response['message'];
}
import sys
import os
import requests  # install requests on python2 using: pip2 install requests

fp = '/path/to/file/you/want/to/upload'

action = data['result']['action']  # action from previous step
cmbs = data['result']['cmbs']  # max file size allowed from previous step
if os.stat(fp).st_size / 1024 / 1024 > cmbs:
    print('File too large')
    sys.exit(3)

try:
    r = requests.post(action, data=open(fp, 'rb'), headers={'Slug': os.path.basename(fp)})
except requests.ConnectionError as e:
    print("Upload Failed with unknown reason", str(e))
else:
    result = r.json()['0']
    print(result['message'])
    if result['ok']:  # Upload was successful
        print(result['url'])
    sys.exit(0)

Output (successful):

{
  "0":{
    "file_name":"MyExampleFile.zip",
    "url":"https://uploadgig.com/file/download/yyyyyyyyyyyyyyyy/MyExampleFile.zip",
    "id":"yyyyyyyyyyyyyyyy",
    "size":526352145,
    "message":"Uploaded was successful, file will be available in a few minutes",
    "ok":true
  }
}

Output (failed):

{
  "0":{
    "message":"Invalid identity provided",
    "ok":false
  }
}

Full upload sample code:

<?php

$username = '[email protected]';
$password = '123';
$fp = '/path/to/file';

$action_api_url = 'https://uploadgig.com/api/get_upload_action';

$options = [
    'http' => [
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query(['user' => $username, 'pass' => $password])
    ]
];

$context  = stream_context_create($options);
$data = json_decode(file_get_contents($action_api_url, false, $context), true);

if ($data['code'] != '1') {
    echo $data['result'], "\n";
    exit(1);
}

$action = $data['result']['action'];
$cmbs = $data['result']['cmbs']  * 1048576;

$stream = fopen($fp, 'rb');

if (filesize($fp) > $cmbs) {
    echo 'File too large';
    exit(2);
}

$ch = curl_init($action);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 60);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 32768);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Transfer-Encoding: chunked', 'Slug: ' . basename($fp)
]);
curl_setopt($ch, CURLOPT_READFUNCTION, function($_, $__, $chunk_size) use (&$stream) {
    return fread($stream, $chunk_size);
});

$response = json_decode(curl_exec($ch), true)[0];
fclose($stream);
curl_close($ch);

if ($response['ok']) {// upload successful
    echo $response['url'];
} else { // upload failed
    echo $response['message'];
}
exit(0);
import sys
import os
import requests  # install requests on python2 using: pip2 install requests

username = '[email protected]'
password = '123'
fp = '/path/to/file'

action_api_url = 'https://uploadgig.com/api/get_upload_action'

r = requests.post(action_api_url, data={
    'user': username,
    'pass': password
}, allow_redirects=True, timeout=10)

try:
    data = r.json()
except requests.ConnectionError:
    print('Unable to get action')
    sys.exit(1)

if data['code'] != '1':
    print(data['result'])
    sys.exit(2)

action = data['result']['action']
cmbs = data['result']['cmbs'] # max file size allowed
if os.stat(fp).st_size/1024/1024 > cmbs:
    print('File too large')
    sys.exit(3)

try:
    r = requests.post(action, data=open(fp, 'rb'), headers={'Slug': os.path.basename(fp)})
except requests.ConnectionError as e:
    print("Upload Failed with unknown reason", str(e))
else:
    result = r.json()['0']

    if result['ok']:  # Upload was successful
        print(result['url'])
    else:  # upload failed
        print(result['message'])
    sys.exit(0)