FastDFS是一款开源的分布式存储框架,主要有三个部分组成,client, tracker和storage.具体的相关架构和介绍见相关博客。文件上传操作实现在FastDFS源代码中的client,名字叫做fdfs_upload_file.c
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
#include
#include
#include
#include
#include
#include
#include
#include "fdfs_client.h"
#include "fastcommon/logger.h"
static void usage(char *argv[])
{
printf("Usage: %s " \
"[storage_ip:port] [store_path_index]\n", argv[0]);
}
int main(int argc, char *argv[])
{
char *conf_filename;//client配置文件
char *local_filename;//需要上传的文件
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
ConnectionInfo *pTrackerServer;//指向跟踪器
int result;
int store_path_index;
ConnectionInfo storageServer;
char file_id[128];
if (argc < 3)
{
usage(argv);
return 1;
}
log_init();
g_log_context.log_level = LOG_ERR;
ignore_signal_pipe();
conf_filename = argv[1];
//客户端配置
if ((result=fdfs_client_init(conf_filename)) != 0)
{
return result;
}
//通过配置文件中读到的数据,连接追踪服务器
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
fdfs_client_destroy();
return errno != 0 ? errno : ECONNREFUSED;
}
local_filename = argv[2];
*group_name = '\0';
if (argc >= 4)
{
const char *pPort;
const char *pIpAndPort;
pIpAndPort = argv[3];
pPort = strchr(pIpAndPort, ':');
if (pPort == NULL)
{
fdfs_client_destroy();
fprintf(stderr, "invalid storage ip address and " \
"port: %s\n", pIpAndPort);
usage(argv);
return 1;
}
storageServer.sock = -1;
snprintf(storageServer.ip_addr, sizeof(storageServer.ip_addr), \
"%.*s", (int)(pPort - pIpAndPort), pIpAndPort);
storageServer.port = atoi(pPort + 1);
if (argc >= 5)
{
store_path_index = atoi(argv[4]);
}
else
{
store_path_index = -1;
}
}
//通过追踪器得到存储节点信息
else if ((result=tracker_query_storage_store(pTrackerServer, \
&storageServer, group_name, &store_path_index)) != 0)
{
fdfs_client_destroy();
fprintf(stderr, "tracker_query_storage fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
return result;
}
//上传操作
result = storage_upload_by_filename1(pTrackerServer, \
&storageServer, store_path_index, \
local_filename, NULL, \
NULL, 0, group_name, file_id);
if (result == 0)
{
printf("%s\n", file_id);
}
else
{
fprintf(stderr, "upload file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
}
tracker_disconnect_server_ex(pTrackerServer, true);
fdfs_client_destroy();
return result;
}
上面是源代码,下面主要利用源代码来实现一个我们自己的文件上传小Demo示例.
fdfs_upload_file.c实现
//filename 客户端配置文件
//myfile 需要上传的文件
int fdfs_upload_file(const char* filename, const char* myfile, char* file_id)
{
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
ConnectionInfo *pTrackerServer;
int result;
int store_path_index;
ConnectionInfo storageServer;
if ((result=fdfs_client_init(filename)) != 0)
{
return result;
}
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
fdfs_client_destroy();
return errno != 0 ? errno : ECONNREFUSED;
}
*group_name = '\0';
if ((result=tracker_query_storage_store(pTrackerServer, \
&storageServer, group_name, &store_path_index)) != 0)
{
fdfs_client_destroy();
fprintf(stderr, "tracker_query_storage fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
return result;
}
result = storage_upload_by_filename1(pTrackerServer, \
&storageServer, store_path_index, \
myfile, NULL, \
NULL, 0, group_name, file_id);
if (result == 0)
{
printf("%s\n", file_id);
}
else
{
fprintf(stderr, "upload file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
}
tracker_disconnect_server_ex(pTrackerServer, true);
fdfs_client_destroy();
return result;
}
相关的头文件fdfs_api.h
#ifndef _FDFS_API_H//防卫式声明,避免重复包含
#define _FDFS_API_H
int fdfs_upload_file(const char* conf_file, const char* myfile, char* file_id);
#endif
主文件main.c
#include
#include
#include
#include"fdfs_api.h"
int main(int args , char* argv[]){
char fileid[1024] = {0};
fdfs_upload_file("/etc/fdfs/client.conf",argv[1],fileid);
printf("fileid = %s\n",fileid);
return 0;
}
接下来在命令行利用gcc编译得到可执行文件,在编译过程中,编译器会提示缺少相关的头文件和动态链接库。
gcc fdfs_api.c main.c -I /usr/include/fastdfs/ -I /usr/include/fastcommon/ -lfdfsclient -o main
这里生成的可执行文件是main,然后运行main
./main main.c
这里将main.c文件上传。最后得到输出信息。
group1/M00/00/00/wKizgFyM_5uALngQAAAA8yoERFY67844.c
fileid = group1/M00/00/00/wKizgFyM_5uALngQAAAA8yoERFY67844.c
这里的group1是配置文件中设置的,M00是地址映射,也就是自己在配置文件中所设置的存储路径。
路漫漫其修远兮,吾将上下而求索。自己给自己打气。