00001 00002 #include <ctype.h> 00003 #include <getopt.h> 00004 #include <stdio.h> 00005 #include <stdlib.h> 00006 #include <string.h> 00007 #include <strings.h> 00008 #include <sys/stat.h> 00009 #include <sys/types.h> 00010 #include <time.h> 00011 #include <unistd.h> 00012 #include "libs3.h" 00013 00014 static int statusG = 0; 00015 00016 typedef struct put_object_callback_data 00017 { 00018 FILE *infile; 00019 uint64_t contentLength, originalContentLength; 00020 } put_object_callback_data; 00021 00022 static void responseCompleteCallback(S3Status status, 00023 const S3ErrorDetails *error, 00024 void *callbackData) 00025 { 00026 int i; 00027 00028 statusG = status; 00029 if (error && error->message) { 00030 printf(" Message: %s\n", error->message); 00031 } 00032 if (error && error->resource) { 00033 printf(" Resource: %s\n", error->resource); 00034 } 00035 if (error && error->furtherDetails) { 00036 printf(" Further Details: %s\n", error->furtherDetails); 00037 } 00038 if (error && error->extraDetailsCount) { 00039 printf("%s", " Extra Details:\n"); 00040 00041 for (i = 0; i < error->extraDetailsCount; i++) { 00042 printf(" %s: %s\n", 00043 error->extraDetails[i].name, 00044 error->extraDetails[i].value); 00045 } 00046 } 00047 00048 00049 } 00050 00051 static S3Status responsePropertiesCallback(const S3ResponseProperties *properties, 00052 void *callbackData) 00053 { 00054 00055 return S3StatusOK; 00056 } 00057 00058 static int putObjectDataCallback(int bufferSize, char *buffer, 00059 void *callbackData) 00060 { 00061 put_object_callback_data *data = 00062 (put_object_callback_data *) callbackData; 00063 int length; 00064 int ret = 0; 00065 00066 if (data->contentLength) { 00067 int length = ((data->contentLength > (unsigned) bufferSize) ? 00068 (unsigned) bufferSize : data->contentLength); 00069 ret = fread(buffer, 1, length, data->infile); 00070 } 00071 data->contentLength -= ret; 00072 return ret; 00073 } 00074 00075 00076 int putFileIntoS3(char *fileName, char *s3ObjName) 00077 { 00078 00079 S3Status status; 00080 char *key; 00081 struct stat statBuf; 00082 uint64_t fileSize; 00083 FILE *fd; 00084 char *accessKeyId; 00085 char *secretAccessKey; 00086 put_object_callback_data data; 00087 00088 00089 accessKeyId = getenv("S3_ACCESS_KEY_ID"); 00090 if (accessKeyId == NULL) { 00091 printf("S3_ACCESS_KEY_ID environment variable is undefined"); 00092 return(-1); 00093 } 00094 00095 secretAccessKey = getenv("S3_SECRET_ACCESS_KEY"); 00096 if (secretAccessKey == NULL) { 00097 printf("S3_SECRET_ACCESS_KEY environment variable is undefined"); 00098 return(-1); 00099 } 00100 00101 key = (char *) strchr(s3ObjName, '/'); 00102 if (key == NULL) { 00103 printf("S3 Key for the Object Not defined\n"); 00104 return(-1); 00105 } 00106 *key = '\0'; 00107 key++; 00108 if (stat(fileName, &statBuf) == -1) { 00109 printf("Unknown input file"); 00110 return(-1); 00111 } 00112 fileSize = statBuf.st_size; 00113 00114 fd = fopen(fileName, "r" ); 00115 if (fd == NULL) { 00116 printf("Unable to open input file"); 00117 return(-1); 00118 } 00119 data.infile = fd; 00120 00121 00122 S3BucketContext bucketContext = 00123 {s3ObjName, 1, 0, accessKeyId, secretAccessKey}; 00124 S3PutObjectHandler putObjectHandler = 00125 { 00126 { &responsePropertiesCallback, &responseCompleteCallback }, 00127 &putObjectDataCallback 00128 }; 00129 00130 00131 if ((status = S3_initialize("s3", S3_INIT_ALL)) 00132 != S3StatusOK) { 00133 printf("Failed to initialize libs3: %s\n",S3_get_status_name(status)); 00134 return(-1); 00135 } 00136 00137 S3_put_object(&bucketContext, key, fileSize, NULL, 0, 00138 &putObjectHandler, &data); 00139 if (statusG != S3StatusOK) { 00140 printf("Put failed: %i\n", statusG); 00141 S3_deinitialize(); 00142 return(-1); 00143 } 00144 S3_deinitialize(); 00145 00146 fclose(fd); 00147 return(0); 00148 } 00149 00150 int main(int argc, char **argv) 00151 { 00152 int i; 00153 if (argc != 3) { 00154 printf("usage: %s fileName s3Objname\n", argv[0]); 00155 exit(-1); 00156 } 00157 i = putFileIntoS3(argv[1], argv[2]) ; 00158 exit(i); 00159 }