Upload question

Last post 11-16-2007 7:32 AM by BarryR. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 09-15-2007 9:26 AM

    Upload question

    Locked Reply Contact

    I have a problem adopting the code examples for PHP to my own code base. I want to add the IMFS to my pluggable storage system. And as such i need the ability to upload the file not through a PHP form, but through a serverside POST request.

    I have no problems obtaining an uploadnode (usually node1.nirvanix.com) and a uploadToken, but when i try to use that host as target for my next post i get a 403 error. If i however take that same URL i use in my post and paste it into my browser, i do get a valid XML response! Is there some time constraints between the retrieval of the uploadNode request and the actual Upload.sh request?

     Does someone have an example of a putFile() request like i describe?

     


     

     

  • 09-17-2007 7:17 PM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 614

    Re: Upload question

    Locked Reply Contact

    Peter Eussen:

    I have no problems obtaining an uploadnode (usually node1.nirvanix.com) and a uploadToken, but when i try to use that host as target for my next post i get a 403 error. If i however take that same URL i use in my post and paste it into my browser, i do get a valid XML response! Is there some time constraints between the retrieval of the uploadNode request and the actual Upload.sh request?

    The Upload token is valid for 5 minutes for the first byte and 72 hours for the last byte.  The 72 hour time period is more for the SOAP AppendFile method where you can upload the file in multiple parts.  My suggestion is to call GetUploadNode before every upload.  In the case of a web form you may want to do a call back to your system that gets the node just before submitting via a hidden form.

    Peter Eussen:

     Does someone have an example of a putFile() request like i describe?



    Here is a simple PHP File upload example:

    <?php

    // Curl method to post a file.
    function postFile($restlink, $file, $timeout = 20) {
        $crl = curl_init();
        if ( !$crl ) return NULL;

        // Set the other parameters on the $restlink to simplify this example.
        // somefile can be any value that does not conflict with an existing parameter,
        // the @$file tells curl to open the file at $file (must be a full path) and stream
        // it to the recipient.
        $postfields = array(
            'somefile'=>"@$file"      
        );
       
        // Set options
        curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($crl, CURLOPT_URL, $restlink);
        curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($crl, CURLOPT_POST, 1);
        curl_setopt($crl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($crl, CURLOPT_POSTFIELDS, $postfields);
        curl_setopt($crl, CURLOPT_VERBOSE, 1);

        // excute curl request
        $result = curl_exec($crl);
       
        // If there was an error print the result.
        if (curl_errno($crl)) {
            print curl_error($crl);
        }
       
        // Close the curl library
        curl_close ($crl);

        return $result;
    }

    // Setup auth variables
    $appkey = "88888888-4444-4444-4444-888888888888";
    $username="someuser";
    $password = "somepassword";

    // Location to upload the file.
    $folder = "upload/";

    // Root website
    $request_root = "https://services.nirvanix.com";

    //Execute Login to get session Token
    $request_login = "$request_root/ws/Authentication/Login.ashx?appKey=$appkey&username=$username&password=$password";       
    $xml = new SimpleXMLElement(file_get_contents($request_login));
    print_r($xml);
    $sessiontoken = $xml->SessionToken;

    // Get the upload node for the file to be uploaded.
    $request_getuploadnode = "$request_root/ws/IMFS/GetUploadNode.ashx?sessionToken=$sessiontoken&filePath=$folder&sizeBytes=10000";
    $xml = new SimpleXMLElement(file_get_contents($request_getuploadnode));
    print_r($xml);
    $ipaddress = $xml->GetUploadNode->IPAddress;
    $uploadtoken = $xml->GetUploadNode->AccessToken;

    // include the full path to the file.  We don't send the data itself, curl will take the file and
    // stream it automatically, on the array inside the request you must include an @ symbol.
    $filename = "d:\sometestfile.txt";

    // Set the additional parameters on the url.
    $request_uploadfile = "http://$ipaddress/Upload.ashx?uploadToken=$uploadtoken&destFolderPath=$folder";
    // Post the file
    $result = postFile($request_uploadfile, $filename);

    print "Result: $result";

    ?>


     

    IM Support (Feel free to add me)

    MSN: barryruffner@msn.com
    Gmail: barryruffner@gmail.com
  • 09-17-2007 8:40 PM In reply to

    Re: Upload question

    Locked Reply Contact

    Thanks for your extensive reply. I managed to get it working as well. I was trying to use the file upload (CURLOPT_UPLOAD etc), but that didn't seem to do the trick. I guess the magic is in the @ feature!

     

    Time to move on to the next method in my api class ;)
     

     

  • 09-24-2007 3:24 PM In reply to

    Invalid Path error

    Locked Reply Contact

    Hi ,

                  I am using  Reference Application: PHP File System Manager . I am getting error 'Invalid Path' while trying to access https://services.nirvanix.com/ws/Authentication/Login.ashx?appKey=$appkey&username=$username&password=$password";

    . But file uploading is working  fine. Hope you can help me?... Please ...... 

  • 09-24-2007 3:29 PM In reply to

    Re: Upload question

    Locked Reply Contact

     

  • 09-24-2007 4:41 PM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 614

    Re: Invalid Path error

    Locked Reply Contact

     I'm a bit confused by the error message your receiving, "Invalid path" normally will occur when doing an IMFS operation that is invalid such as specifying a path that doesn't exist as a source path or invalid characters or similar.  The command you have listed above is a HTTPS Login.  Perhaps you can replace the $appKey, $username and $password and see if the link directly through a browser will return the result your expecting.

    Let me know,
        Barry R.
     

    IM Support (Feel free to add me)

    MSN: barryruffner@msn.com
    Gmail: barryruffner@gmail.com
  • 11-15-2007 1:10 PM In reply to

    • srinu
    • Top 100 Contributor
    • Joined on 11-06-2007
    • Posts 2

    Re: Upload question

    Locked Reply Contact

    Hi, 

    I want full path of the image. So i can store that path in my database.

    Can you please give me curl through return full path code.

    Is there anyway through curl , we can get the fullpath of the uploaded image. 

     

  • 11-15-2007 5:44 PM In reply to

    Re: Upload question

    Locked Reply Contact

    What i used to obtain a link was using the /ws/IMFS/GetDownloadLinks.ashx call. This call gives you a download link from where you can download the file. Downside is  that this link requires a expiration date. I have not found another way to get a permanent download link :(

     This would make the service more useable though... Nirvanix? ;)

     

    -- Edit

     

    Pardon, you probably need CreateHostedItem call.. the PDF is not clear, but the sample REST reply shows that you get a download link, though the documentation does not show it in the Output value list
     

  • 11-15-2007 11:17 PM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 614

    Re: Upload question

    Locked Reply Contact

    srinu:

    I want full path of the image. So i can store that path in my database.

    Can you please give me curl through return full path code.

    Is there anyway through curl , we can get the fullpath of the uploaded image.

     

    On the sample code above the $return variable will return the response which contains the size bytes of the file uploaded and any errors that occured.  If you have called the method above then you would have provided the full path along with the filename. The path in the account would be &destFolderPath=$folder which is passed in the URL.  The $filename which is passed directly to the method lets you set the exact filename.

    Let me know if you have any further questions about this. 

     

    Peter Eussen:

    What i used to obtain a link was using the /ws/IMFS/GetDownloadLinks.ashx call. This call gives you a download link from where you can download the file. Downside is  that this link requires a expiration date. I have not found another way to get a permanent download link :(

    You are correct that expiration needs to be provided.  However, the expiration given can be quite a long time in the future making it a nearly permanent link.  The problem with this method is the inability to disable the link if you no longer wish to share it out.  This is a good argument for the CreateHostedItem which you describe as well.

     

    Peter Eussen:

     Pardon, you probably need CreateHostedItem call.. the PDF is not clear, but the sample REST reply shows that you get a download link, though the documentation does not show it in the Output value list

    The CreateHostedItem allows you to set a folder or file (Recursive to the children, files and folders) to be public.  In this case the construction of the link is up to you since it can be constructed without a token. 

    To build the path to a particular file you will need to call GetDownloadNodes to retrieve the node where the file is best served from or simply point them at services.nirvanix.com to automatically redirect.  You need the account, username and full path as shown below:

     

     http://nodeX.nirvanix.com/ApplicationName/Username/Path/file.txt


     

    The node in this case is retrieved by the GetDownloadNodes or you can simply build it as http://services.nirvanix.com/appname/username/path/file.txt but will incur a redirection.

    I hope this clears things up.

    Regards,
         Barry R.

    IM Support (Feel free to add me)

    MSN: barryruffner@msn.com
    Gmail: barryruffner@gmail.com
    Filed under: , ,
  • 11-16-2007 5:22 AM In reply to

    • srinu
    • Top 100 Contributor
    • Joined on 11-06-2007
    • Posts 2

    Re: Upload question

    Locked Reply Contact

    Can you please give me  sample code for full path for while uploading the file through above curl code.

     

    Otherwise Please provide me sample code for uploading and downloading link.

     

    Thanks

    Srinivas

  • 11-16-2007 7:32 AM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 614

    Re: Upload question

    Locked Reply Contact

    When doing an upload your application is providing both the path and filename so I cannot give this information.

    As for download, I'm not sure I can help as the PHP File Manager already does this in the Browse.php.  Please review the code as these topics have already been covered in the existing samples.

    The HostedFileManager is a very direct example of this, this lets you upload a file and get a link to the file uploaded.  You can download these applications at:

    http://developer.nirvanix.com/files/folders/php/default.aspx

    Since this topic has wandered too far off course I'm going to lock this thread.  Please start a new thread if you have additional questions about uploads and downloads after reviewing the existing applications.

    Best regards,
        Barry R.
     

    IM Support (Feel free to add me)

    MSN: barryruffner@msn.com
    Gmail: barryruffner@gmail.com
Page 1 of 1 (11 items)