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) Download Object from Google Cloud StorageDemonstrates how to download an object from Google Cloud Storage to a file.
#include <C_CkStringBuilderW.h> #include <C_CkHttpW.h> #include <C_CkBinDataW.h> void ChilkatSample(void) { HCkStringBuilderW sbToken; HCkHttpW http; const wchar_t *url; HCkBinDataW fileData; BOOL success; HCkStringBuilderW sbErrorResponse; int responseCode; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This example uses a previously obtained access token having permission for the // scope "https://www.googleapis.com/auth/cloud-platform" // In this example, Get Google Cloud Storage OAuth2 Access Token, // the service account access token was saved to a text file. This example fetches the access token from the file.. sbToken = CkStringBuilderW_Create(); CkStringBuilderW_LoadFile(sbToken,L"qa_data/tokens/googleCloudStorageAccessToken.txt",L"utf-8"); // Send a GET equivalent to this curl command. // curl -X GET \ // -H "Authorization: Bearer [OAUTH2_TOKEN]" \ // -o "[SAVE_TO_LOCATION]" \ // "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]?alt=media" http = CkHttpW_Create(); CkHttpW_putAuthToken(http,CkStringBuilderW_getAsString(sbToken)); // Construct a URL to download an object named "starfish.jpg" from the "chilkat-ocean" bucket. CkHttpW_SetUrlVar(http,L"bucket_name",L"chilkat-ocean"); CkHttpW_SetUrlVar(http,L"object_name",L"starfish.jpg"); url = L"https://www.googleapis.com/storage/v1/b/{$bucket_name}/o/{$object_name}?alt=media"; // If there is an error response, then we didn't actually download the file data, // but instead we downloaded an error response.. fileData = CkBinDataW_Create(); success = CkHttpW_DownloadBd(http,url,fileData); if (success == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkStringBuilderW_Dispose(sbToken); CkHttpW_Dispose(http); CkBinDataW_Dispose(fileData); return; } sbErrorResponse = CkStringBuilderW_Create(); responseCode = CkHttpW_getLastStatus(http); if (responseCode == 401) { wprintf(L"If invalid credentials, then it is likely the access token expired.\n"); wprintf(L"Your app should automatically fetch a new access token and re-try.\n"); } if (responseCode != 200) { // Get the error response CkStringBuilderW_AppendBd(sbErrorResponse,fileData,L"utf-8",0,0); wprintf(L"Error response code = %d\n",responseCode); wprintf(L"Error:\n"); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbErrorResponse)); CkStringBuilderW_Dispose(sbToken); CkHttpW_Dispose(http); CkBinDataW_Dispose(fileData); CkStringBuilderW_Dispose(sbErrorResponse); return; } wprintf(L"Success.\n"); // Save the downloaded data to a file. success = CkBinDataW_WriteFile(fileData,L"qa_output/starfish.jpg"); CkStringBuilderW_Dispose(sbToken); CkHttpW_Dispose(http); CkBinDataW_Dispose(fileData); CkStringBuilderW_Dispose(sbErrorResponse); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.