int main()
{
	//unsigned short retcode;			//保存服务器http返回码的解析结果;
	//const char *retinfo;            //保存通过retcode获得的错误信息

	oss_client_t *client = client_initialize_with_endpoint(access_id, access_key, endpoint);
	const char *bucket_name = "bucket_example";       //设置bucket_name
	const char *key = "a.txt";

	time_t t = time(NULL); 
	t += 60;
	char  expiration[20];
	sprintf(expiration, "%ld", t);
	printf("expiration = %s\n", expiration);
	
	char * url = client_generate_presigned_url_with_expiration(client, bucket_name, key, expiration);
	printf("url = %s\n", url);
	if(url != NULL) {
		free(url);
		url = NULL;
	}

	client_finalize(client);
	return 0;
}
Exemplo n.º 2
0
char *
client_generate_presigned_url_with_method(oss_client_t *client,
		const char *bucket_name,
		const char *key,
		const char *expiration,
		const char *method)
{
	assert(client != NULL);
	assert(bucket_name != NULL);
	assert(key != NULL);
	assert(expiration != NULL);
	if((method == NULL) || !strcmp(method, ""))
		return client_generate_presigned_url_with_expiration(client,
				bucket_name, key, expiration);

	unsigned int bucket_name_len = strlen(bucket_name);
	unsigned int key_len = strlen(key);
	char *url = (char *)malloc(sizeof(char) * (bucket_name_len + key_len) + 256);
	char *resource = (char *)malloc(sizeof(char) * (bucket_name_len + key_len) + 16);
	unsigned int sign_len = 0;
	oss_map_t * default_headers = oss_map_new(1);

	/* *
	 * 构造各参数
	 */
	sprintf(resource, "/%s/%s", bucket_name, key);
	oss_map_put(default_headers, OSS_DATE, expiration);

	/**
	 * 生成签名值
	 */
	char *sign = generate_authentication(client->access_key, method,
			default_headers, NULL, resource, &sign_len);

	sprintf(url, "http://%s%s?OSSAccessKeyId=%s&Expires=%s&Signature=%s", client->endpoint, resource, client->access_id, expiration, sign);
	
	oss_map_delete(default_headers);
	if(sign != NULL) {
		free(sign);
		sign = NULL;
	}
	if(resource != NULL) {
		free(resource);
		resource = NULL;
	}
	
	return url;

}