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