NOS Storage

Update time: 2023/03/24 03:15:03

Netease Object Storage (NOS) provides you with cloud-based data storage services. By using NOS, you can store various types of files, such as text, images, and audio and video in the NOS system and access them securely at any time through the network.

Download

Download resources. The callback function returns the download result and download progress.

Callback that returns the download result.

c@param rescode: code 200 indicates success
@param file_path: full path of the downloaded file.
@param call_id: Session ID
@param res_id: message ID. Together with the session ID, you can locate the specific message, and then adjust the UI presentation according to the rescode.
typedef void(*nim_nos_download_cb_func)(int rescode, const char *file_path, const char *call_id, const char *res_id, const char *json_extension, const void *user_data);

Callback for the download progress

c@param downloaded_size: the size of downloaded data
@param file_size: file size in bytes
typedef void(*nim_nos_download_prg_cb_func)(__int64 downloaded_size, __int64 file_size, const char *json_extension, const void *user_data);

Sample code:

C++
c++void DownloadResourceCallback(nim::NIMResCode res_code, const std::string& file_path, const std::string& call_id, const std::string& res_id)
{

}

void DownloadResourceProgressCallback(__int64 downloaded_size, __int64 file_size)
{

}

void foo(const IMMessage& msg)
{
	nim::NOS::FetchMedia(msg, &DownloadResourceCallback, &DownloadResourceProgressCallback);
}
C
cvoid CallbackDownload(int res_code, const char *file_path, const char *call_id, const char *res_id, const char *json_extension, const void *user_data)
{
	···
}

void CallbackProgress(__int64 completed_size, __int64 total_size, const char *json_extension, const void *callback)
{
	···
}

typedef void(*nim_nos_download_media)(const char *json_msg, nim_nos_download_cb_func callback_result, const void *download_user_data, nim_nos_download_prg_cb_func prg_cb, const void *prg_user_data);

void foo(const IMMessage& msg)
{
	nim_nos_download_media func = (nim_nos_download_media) GetProcAddress(hInst, "nim_nos_download_media");
	func(msg.ToJsonString(false).c_str(), &CallbackDownload, nullptr, &CallbackProgress, nullptr);
}

Download and pause

Starting from v3.3.0, the SDK has added an extension interface for downloading, which supports resuming uploads from pauses. You can set a custom timeout period and the local path of downloaded files, and get the instantaneous download speed and average download speed.

C++
c++//Callback for the download result
void CallbackDownload(nim::NIMResCode res_code, const nim::DownloadMediaResult& result)
{
	if (res_code == kNIMResSuccess)
	{
		//Download success
	}
	else if (res_code == kNIMLocalResMsgNosDownloadCancel)
	{
		//Download pause
	}
	else
	{
		/ download failed
	}
}

//Callback for the download progress, parameters: size of downloaded data, file size, and other download information
void CallbackHttpPro(__int64 completed_size, __int64 file_size, const ProgressData& data)
{
	...
}

//Callback for the instantaneous download speed
void CallbackSpeed(__int64 speed)
{
	if (speed > 0)
	{
		char res[1024];
		sprintf_s(res, "speed : %lld B/s(%lf KB/s)", speed, (static_cast<double>(speed))/1024);
	}
}

static __int64 actual_size_ = 0;
//Callback for download information, parameters: size of the downloaded file and the average download speed.
void CallbackTransferInfo(__int64 actual_size, __int64 speed)
{	
	actual_size_ += actual_size;
	char res[1024];
	sprintf_s(res, "average speed : %lld(%lf KB/s)", speed, (static_cast<double>(speed))/1024);

	sprintf_s(res, "actual size : %lld, block size : %lld", actual_size_, actual_size);
}

//Start a download task
void foo_start_http_download(const std::string& task_uuid)
{
	Json::FastWriter writer;
	Json::Value value;
	value[kNIMNosLowLimit] = ; //int(optional) HTTP generic configuration, transmission speed, bytes per second (default value 10).
	value[kNIMNosLowTime] = ;  //int(optional) HTTP generic configuration. If the transmission speed is less than low_limit (bytes per second) within the low_time seconds, the download task will be canceled due to timeout (default value 60)
	value[kNIMNosTimeout] = ; //int(optional) HTTP generic configuration, timeout time in milliseconds. The minimum value for download is 10000 and 30000 for upload. By default 30000 if unspecified.
	value[kNIMNosSaveAsFilePath] = ; //String(optional) The local path of a downloaded file for HTTP download tasks. If unspecified, the default pash is returned.
	value[kNIMNosFileSize] = ; //int64 (required for resuming download from pauses) File size, required for resuming download in bytes
	value[]=;/ UUID, required for resumable uploads and downloads/ HTTP value[kNIMNosTaskId] = task_uuid;   //String (required for resuming download) HTTP generic configuration, task ID. If the specified ID is an unfinished transmission task, it will resume the download (the ID must be unique).
	nim::NOS::DownloadResourceEx(url, writer.write(value), &CallbackDownload, &CallbackHttpPro, &CallbackSpeed, &CallbackTransferInfo);
}

//Pause a download task
void foo_pause_http_download(const std::string& task_uuid)
{
	nim::NOS::StopDownloadResourceEx(task_uuid);
}
C
c//Callback for the download result
static void CallbackDownloadEx(int res_code, const char *file_path, const char *call_id, const char *res_id, const char *json_extension, const void *user_data)
{
	if (res_code == kNIMResSuccess)
	{
		//Download success
	}
	else if (res_code == kNIMLocalResMsgNosDownloadCancel)
	{
		//Download pause
	}
	else
	{
		/ download failed
	}
}

//Callback for the download progress, parameters: size of downloaded data, file size, and other download information
static void CallbackProgressEx(int64_t completed_size, int64_t total_size, const char *json_extension, const void *callback)
{
	···
}

//Callback for the instantaneous download speed
static void CallbackSpeed(int64_t speed, const char *json_extension, const void *callback)
{
	if (speed > 0)
	{
		char res[1024];
		sprintf_s(res, "speed : %lld B/s(%lf KB/s)", speed, (static_cast<double>(speed))/1024);
	}
}

static __int64 actual_size_ = 0;
//Callback for download information, parameters: size of the downloaded file and the average download speed.
static void CallbackTransferInfo(int64_t actual_size, int64_t speed, const char *json_extension, const void *callback)
{
	actual_size_ += actual_size;
	char res[1024];
	sprintf_s(res, "average speed : %lld(%lf KB/s)", speed, (static_cast<double>(speed))/1024);

	sprintf_s(res, "actual size : %lld, block size : %lld", actual_size_, actual_size);
}

typedef void(*nim_nos_download_ex)(const char *nos_url, const char *json_extension, nim_nos_download_cb_func callback_result, const void *res_user_data, nim_nos_download_prg_cb_func prg_cb, const void *prg_user_data, nim_nos_download_speed_cb_func speed_cb, const void *speed_user_data, nim_nos_download_info_cb_func info_cb, const void *info_user_data);

typedef void(*nim_nos_stop_download_ex)(const char *task_id, const char *json_extension);

//Start a download task
void foo_start_http_download(const std::string& task_uuid)
{
	Json::FastWriter writer;
	Json::Value value;
	value[kNIMNosLowLimit] = ; //int(optional) HTTP generic configuration, transmission speed, bytes per second (default value 10).
	value[kNIMNosLowTime] = ;  //int(optional) HTTP generic configuration. If the transmission speed is less than low_limit (bytes per second) within the low_time seconds, the download task will be canceled due to timeout (default value 60)
	value[kNIMNosTimeout] = ; //int(optional) HTTP generic configuration, timeout time in milliseconds. The minimum value for download is 10000 and 30000 for upload. By default 30000 if unspecified.
	value[kNIMNosSaveAsFilePath] = ; //String(optional) The local path of a downloaded file for HTTP download tasks. If unspecified, the default pash is returned.
	value[kNIMNosFileSize] = ; //int64 (required for resuming download from pauses) File size, required for resuming download in bytes
	value[]=;/ UUID, required for resumable uploads and downloads/ HTTP value[kNIMNosTaskId] = task_uuid;   //String (required for resuming download) HTTP generic configuration, task ID. If the specified ID is an unfinished transmission task, it will resume the download (the ID must be unique).

	nim_nos_download_ex func = (nim_nos_download_ex) GetProcAddress(hInst, "nim_nos_download_ex");
	func(nos_url.c_str(), writer.write(value).c_str(), &CallbackDownloadEx, callback_result_userdata, &CallbackProgressEx, callback_progress_pointer, &CallbackSpeed, callback_speed_pointer, &CallbackTransferInfo, callback_transfer_pointer);
}	

//Pause a download task
void foo_pause_http_download(const std::string& task_uuid)
{
	nim_nos_stop_download_ex func = (nim_nos_stop_download_ex) GetProcAddress(hInst, "nim_nos_stop_download_ex");
	func(task_uuid.c_str(), nullptr);
}

Upload

Upload resources. The callback function returns the upload result and upload progress.

Callback for the upload result

c@param rescode: The error code, code 200 indicates success
@param url: The URL returned after the upload is successful
typedef void(*nim_nos_upload_cb_func)(int rescode, const char *url, const char *json_extension, const void *user_data);

Callback for upload progress

c@uploaded_size: size of uploaded data
@file_size: file size
typedef void(*nim_nos_upload_prg_cb_func)(__int64 uploaded_size, __int64 file_size, const char *json_extension, const void *user_data);	

Sample code:

C++
c++void OnUploadCallback(int res_code, const std::string& url)
{

}

void foo()
{
	nim::NOS::UploadResource("D://test_file.txt", &OnUploadCallback);
}
C
cvoid CallbackUpload(int res_code, const char *url, const char *json_extension, const void *user_data)
{
	···
}

void CallbackProgress(__int64 completed_size, __int64 total_size, const char *json_extension, const void *callback)
{
	···
}

typedef void(*nim_nos_upload)(const char *local_file, nim_nos_upload_cb_func callback_result, const void *res_user_data, nim_nos_upload_prg_cb_func prg_cb, const void *prg_user_data);

void foo()
{
	nim_nos_upload func = (nim_nos_upload) GetProcAddress(hInst, "nim_nos_upload");
	func("D://test_file.txt", &CallbackUpload, nullptr, &CallbackProgress, nullptr);
}

Upload and pause

Starting from v3.3.0, the SDK has added an extension interface for upload, which supports resuming uploads from pauses. You can set a custom timeout period, and get the instantaneous upload speed and average upload speed.

C++
c++//Callback for the upload result
void CallbackUpload(nim::NIMResCode res_code, const nim::DownloadMediaResult& result)
{
	if (res_code == kNIMResSuccess)
	{
		//Upload success
	}
	else if (res_code == kNIMLocalResMsgNosUploadCancel)
	{
		//Upload pause
	}
	else
	{
		// Upload failure
	}
}

//Callback for the upload progress, parameters: size of uploaded data, file size, and other upload information
void CallbackHttpPro(__int64 completed_size, __int64 file_size, const ProgressData& data)
{
	...
}

//Callback for the instantaneous upload speed
void CallbackSpeed(__int64 speed)
{
	if (speed > 0)
	{
		char res[1024];
		sprintf_s(res, "speed : %lld B/s(%lf KB/s)", speed, (static_cast<double>(speed))/1024);
	}
}

static __int64 actual_size_ = 0;
//Callback for upload information, parameters: size of the upload file and the average upload speed.
void CallbackTransferInfo(__int64 actual_size, __int64 speed)
{	
	actual_size_ += actual_size;
	char res[1024];
	sprintf_s(res, "average speed : %lld(%lf KB/s)", speed, (static_cast<double>(speed))/1024);

	sprintf_s(res, "actual size : %lld, block size : %lld", actual_size_, actual_size);
}

//Start an upload task
void foo_start_http_upload(const std::string& task_uuid)
{
	Json::FastWriter writer;
	Json::Value value;
	value[kNIMNosLowLimit] = ; //int(optional) HTTP generic configuration, transmission speed, bytes per second (default value 10).
	value[kNIMNosLowTime] = ;  //int(optional) HTTP generic configuration. If the transmission speed is less than low_limit (bytes per second) within the low_time seconds, the download task will be canceled due to timeout (default value 60)
	value[kNIMNosTimeout] = ; //int(optional) HTTP generic configuration, timeout time in milliseconds. The minimum value for download is 10000 and 30000 for upload. By default 30000 if unspecified.
	value[kNIMNosNeedContinueTrans] = ; //bool, set the value to true for resuming download
	value[]=;/ UUID, required for resumable uploads and downloads/ HTTP value[kNIMNosTaskId] = task_uuid;   //String (required for resuming download) HTTP generic configuration, task ID. If the specified ID is an unfinished transmission task, it will resume the download (the ID must be unique).
	nim::NOS::UploadResourceEx(file_path, writer.write(value), &CallbackUpload, &CallbackHttpPro, &CallbackSpeed, &CallbackTransferInfo);
}

//Pause an upload task
void foo_pause_http_upload(const std::string& task_uuid)
{
	nim::NOS::StopUploadResourceEx(task_uuid);
}
C
c//Callback for the upload result
static void CallbackUploadEx(int res_code, const char *url, const char *json_extension, const void *user_data)
{
	if (res_code == kNIMResSuccess)
	{
		//Download success
	}
	else if (res_code == kNIMLocalResMsgNosDownloadCancel)
	{
		//Download pause
	}
	else
	{
		/ download failed
	}
}

//Callback for the upload progress, parameters: size of uploaded data, file size, and other upload information
static void CallbackProgressEx(int64_t completed_size, int64_t total_size, const char *json_extension, const void *callback)
{
	···
}

//Callback for the instantaneous upload speed
static void CallbackSpeed(int64_t speed, const char *json_extension, const void *callback)
{
	if (speed > 0)
	{
		char res[1024];
		sprintf_s(res, "speed : %lld B/s(%lf KB/s)", speed, (static_cast<double>(speed))/1024);
	}
}

static __int64 actual_size_ = 0;
//Callback for upload information, parameters: size of the upload file and the average upload speed.
static void CallbackTransferInfo(int64_t actual_size, int64_t speed, const char *json_extension, const void *callback)
{
	actual_size_ += actual_size;
	char res[1024];
	sprintf_s(res, "average speed : %lld(%lf KB/s)", speed, (static_cast<double>(speed))/1024);

	sprintf_s(res, "actual size : %lld, block size : %lld", actual_size_, actual_size);
}

typedef void(*nim_nos_upload_ex)(const char *local_file, const char *json_extension, nim_nos_upload_cb_func callback_result, const void *res_user_data, nim_nos_upload_prg_cb_func prg_cb, const void *prg_user_data, nim_nos_upload_speed_cb_func speed_cb, const void *speed_user_data, nim_nos_upload_info_cb_func info_cb, const void *info_user_data);

typedef void(*nim_nos_stop_upload_ex)(const char *task_id, const char *json_extension);


//Start an upload task
void foo_start_http_download(const std::string& task_uuid)
{
	Json::FastWriter writer;
	Json::Value value;
	value[kNIMNosLowLimit] = ; //int(optional) HTTP generic configuration, transmission speed, bytes per second (default value 10).
	value[kNIMNosLowTime] = ;  //int(optional) HTTP generic configuration. If the transmission speed is less than low_limit (bytes per second) within the low_time seconds, the download task will be canceled due to timeout (default value 60)
	value[kNIMNosTimeout] = ; //int(optional) HTTP generic configuration, timeout time in milliseconds. The minimum value for download is 10000 and 30000 for upload. By default 30000 if unspecified.
	value[kNIMNosNeedContinueTrans] = ; //bool, set the value to true for resuming download
	value[]=;/ UUID, required for resumable uploads and downloads/ HTTP value[kNIMNosTaskId] = task_uuid;   //String (required for resuming download) HTTP generic configuration, task ID. If the specified ID is an unfinished transmission task, it will resume the download (the ID must be unique).

	nim_nos_upload_ex func = (nim_nos_upload_ex) GetProcAddress(hInst, "nim_nos_upload_ex");
	func(local_file.c_str(), writer.write(value).c_str(), &CallbackUploadEx, callback_result_userdata, &CallbackProgressEx, callback_progress_pointer, &CallbackSpeed, callback_speed_pointer, &CallbackTransferInfo, callback_transfer_pointer);
}	

//Pause an upload task
void foo_pause_http_download(const std::string& task_uuid)
{
	nim_nos_stop_upload_ex func = (nim_nos_stop_upload_ex) GetProcAddress(hInst, "nim_nos_stop_upload_ex");
	func(task_uuid.c_str(), nullptr);
}
Was this page helpful?
Yes
No
  • Download
  • Download and pause
  • Upload
  • Upload and pause