根据下面的文档,你只要在上海万根网络注册了云存储账户,就可以使用libs3生成的动态库,操作云存储了。
http://docs.openfans.org/ceph/ceph4e2d658765876863/ceph-1/ceph-object-gateway3010ceph5bf98c617f5151733011/s3-api/c

libs3在ubuntu下的编译过程:

1 下载源码,github上面最活跃的。

git clone https://github.com/bji/libs3.git

2 安装依赖库。libs3需要libxml2和liburl依赖。

sudo apt-get install libxml2
sudo apt-get install libxml2-dev
sudo apt-get install libcurl4-openssl-dev

如果不安装会提示如下错误。 make: curl-config: Command not found make: curl-config:
Command not found make: xml2-config: Command not found make:
xml2-config: Command not found build/obj/acl.do: Compiling dynamic
object In file included from inc/response_headers_handler.h:32:0,
from inc/request.h:32,
from src/acl.c:30: inc/util.h:30:23: fatal error: curl/curl.h: No such file or directory compilation terminated.
GNUmakefile:216: recipe for target ‘build/obj/acl.do’ failed make: *
[build/obj/acl.do] Error 1

如果很熟悉,可以自己下载libxml2和curl源码自己编译配置。

如果前面make 后出现错误的话,再make时,需要先清理一下,执行:

make clean

否则,会提示如下错误: make: * No rule to make target ‘curl/curl.h’, needed by
‘build/obj/acl.do’. Stop.

3 编译文件,执行 make 命令,生成build目录。
make

cd build/lib
有两个文件 libs3.a libs3.so.2

4 使用。拷贝测试的视频文件a.flv和s3-test.c文件,编译c文件,
gcc s3-test.c -ls3 -g -Wall -o s3-test
执行,效果如图:这里写图片描述

5 测试c文件。
s3-test.c文件

#include "libs3.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define HOST "s-api.yunvm.com"  // s3服务端接口主机
#define KEY "test001"  // 账号
#define SECRET "UtbaZSpla3MJcidldKK9pvBNl6gaklG0BxWNXXXX"  // 安全码
#define BUCKET "video01"  // bucket
#define LOCALFILE "a.flv" // 本地文件
// key,也就是上传到远程后,bucket下的路径和文件名称
#define REMOTEPATH "test/a.flv"

S3Status responsePropertiesCallback(const S3ResponseProperties* properties, void* callbackData)
{
    printf("put properties ok");
    return S3StatusOK;
}

static void responseCompleteCallback(S3Status status, const S3ErrorDetails* error, void* callbackData)
{
    printf("put status:%d\n", status);
    return;
}

typedef struct put_object_callback_data {
    FILE* infile;
    uint64_t contentLength;
}put_object_callback_data;

static int putObjectDataCallback(int bufferSize, char* buffer, void* callbackData)
{
    put_object_callback_data* data = (put_object_callback_data*)callbackData;
    int ret = 0;
    if(data->contentLength){
        int toRead = ((data->contentLength > (unsigned)bufferSize)?(unsigned)bufferSize : data->contentLength);
        ret = fread(buffer, 1, toRead, data->infile);
    }
    data->contentLength -= ret;
    printf("send %d bytes\n", ret);
    return ret;
}

int main(int argc, char** argv)
{
    const char *cacheControl = 0, *contentType = 0, *md5 = 0;
    const char *contentDispositionFilename = 0, *contentEncoding = 0;
    int64_t expires = -1;
    S3CannedAcl cannedAcl = S3CannedAclPublicRead;
    int metaPropertiesCount = 0;
    S3NameValue metaProperties[S3_MAX_METADATA_COUNT];
    memset(metaProperties, 0, sizeof(metaProperties));

    S3BucketContext bucketContext;
    S3ResponseHandler responseHandler;
    S3PutObjectHandler putObjectHandler;
    S3PutProperties S3_property = {
        contentType,
        md5,
        cacheControl,
        contentDispositionFilename,
        contentEncoding,
        expires,
        cannedAcl,
        metaPropertiesCount,
        metaProperties,
    };
    S3_property.contentType = "video/x-flv";

    put_object_callback_data data;
    struct stat statbuf;

    bucketContext.hostName= HOST;
    bucketContext.bucketName = BUCKET;
    bucketContext.protocol = S3ProtocolHTTP;
    bucketContext.uriStyle = S3UriStylePath;
    bucketContext.accessKeyId = KEY;
    bucketContext.secretAccessKey = SECRET;

    responseHandler.propertiesCallback = &responsePropertiesCallback;
    responseHandler.completeCallback = &responseCompleteCallback;

    putObjectHandler.responseHandler = responseHandler;
    putObjectHandler.putObjectDataCallback = &putObjectDataCallback;

    if( 0!= stat(LOCALFILE, &statbuf) ){
        perror("stat local file");
        return -1;
    }
    if(!( data.infile = fopen(LOCALFILE, "r") )){
        perror("open local file");
        return -1;
    }
    data.contentLength = statbuf.st_size;
    printf("file size:%ld\n", data.contentLength);

    S3_initialize("s3", S3_INIT_ALL, HOST);
    S3_put_object(&bucketContext, REMOTEPATH, data.contentLength, &S3_property, NULL, &putObjectHandler, &data);
    fclose(data.infile);
    S3_deinitialize();
    return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐