Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) REST Asynchronous Streaming Upload FileDemonstrates how to asynchronous streaming upload a file to cloud storage. This particular example demonstrates an upload to the Azure Cloud Storage service. The same concepts apply to S3, Google Cloud, and Google Drive.
#include <C_CkRestW.h> #include <C_CkAuthAzureStorageW.h> #include <C_CkStreamW.h> #include <C_CkTaskW.h> void ChilkatSample(void) { HCkRestW rest; BOOL bTls; int port; BOOL bAutoReconnect; BOOL success; HCkAuthAzureStorageW azAuth; HCkStreamW sendStream; HCkTaskW task; int curPctDone; int responseStatusCode; const wchar_t *responseBodyStr; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. rest = CkRestW_Create(); // Connect to the Azure Storage Blob Service bTls = TRUE; port = 443; bAutoReconnect = TRUE; // In this example, the storage account name is "chilkat". success = CkRestW_Connect(rest,L"chilkat.blob.core.windows.net",port,bTls,bAutoReconnect); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkRestW_Dispose(rest); return; } // Provide Azure Cloud credentials for the REST call. azAuth = CkAuthAzureStorageW_Create(); CkAuthAzureStorageW_putAccessKey(azAuth,L"AZURE_ACCESS_KEY"); // The account name used here should match the 1st part of the domain passed in the call to Connect (above). CkAuthAzureStorageW_putAccount(azAuth,L"chilkat"); CkAuthAzureStorageW_putScheme(azAuth,L"SharedKey"); CkAuthAzureStorageW_putService(azAuth,L"Blob"); // This causes the "x-ms-version: 2021-08-06" header to be automatically added. CkAuthAzureStorageW_putXMsVersion(azAuth,L"2021-08-06"); success = CkRestW_SetAuthAzureStorage(rest,azAuth); // Set some request headers. success = CkRestW_AddHeader(rest,L"x-ms-blob-content-disposition",L"attachment; filename=\"thisIsATest.txt\""); success = CkRestW_AddHeader(rest,L"x-ms-blob-type",L"BlockBlob"); success = CkRestW_AddHeader(rest,L"x-ms-meta-m1",L"v1"); success = CkRestW_AddHeader(rest,L"x-ms-meta-m2",L"v2"); // Note: The application does not need to explicitly set the following // headers: x-ms-date, Authorization, and Content-Length. These headers // are automatically set by Chilkat. sendStream = CkStreamW_Create(); // Define the source data for the stream to be a file. CkStreamW_putSourceFile(sendStream,L"qa_data/hamlet.xml"); // Create a background thread task to upload from the stream // The name of the Azure storage container is "test". task = CkRestW_FullRequestStreamAsync(rest,L"PUT",L"/test/thisIsATest.txt",sendStream); // Start the task. success = CkTaskW_Run(task); // In this example, we'll simply sleep and periodically // check to see if the REST upload if finished. curPctDone = 0; while (CkTaskW_getFinished(task) != TRUE) { CkTaskW_SleepMs(task,100); } // Check to see if the call to FullRequestStream in the background thread pool succeeded. if (CkTaskW_getTaskSuccess(task) != TRUE) { // Show what would've been the LastErrorText had FullRequestStream been called synchronously wprintf(L"%s\n",CkTaskW_resultErrorText(task)); CkTaskW_Dispose(task); CkRestW_Dispose(rest); CkAuthAzureStorageW_Dispose(azAuth); CkStreamW_Dispose(sendStream); return; } responseStatusCode = CkRestW_getResponseStatusCode(rest); // When successful, the Azure Storage service will respond with a 201 response code, // with an empty body. Therefore, in the success condition, the responseStr is empty. if (responseStatusCode == 201) { wprintf(L"File uploaded.\n"); } else { // It failed, so examine the response body, if one was returned: // Given that FullRequestStream returns a string, the return value is obtained via GetResultString. responseBodyStr = CkTaskW_getResultString(task); wprintf(L"response body (if any): %s\n",responseBodyStr); // Examine the request/response to see what happened. wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest)); wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest)); wprintf(L"response header: %s\n",CkRestW_responseHeader(rest)); wprintf(L"---\n"); wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest)); wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest)); } CkTaskW_Dispose(task); CkRestW_Dispose(rest); CkAuthAzureStorageW_Dispose(azAuth); CkStreamW_Dispose(sendStream); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.