0

I have an iOS App which uploads and downloads files to Amazon AWS S3 Bucket. I want my app to upload a file if its name doesn't exist. And download file with its name if exist. I found an answer about checking file exist or not. (https://stackoverflow.com/a/27932233/4646247) but there is no example.

Community
  • 1
  • 1
cameloper
  • 297
  • 1
  • 3
  • 16

1 Answers1

0

You can list all the names of the files that exist in a bucket and see if the file you want to upload is already there or not. You can list files by;

- (void) listFilesAWS_S3WithBucketName: (NSString *) bucketName
{
    AWSS3 *s3 = [AWSS3 defaultS3];

    AWSS3ListObjectsRequest *listObjectReq=[AWSS3ListObjectsRequest new];
    listObjectReq.bucket=bucketName;

    [[[s3 listObjects:listObjectReq] continueWithBlock:^id(BFTask *task) 
    {
        if(task.error)
        {
            NSLog(@"the request failed. error %@",task.error);
        }
        else if(task.result)
        {
            AWSS3ListObjectsOutput *listObjectsOutput=task.result;
            NSArray * files = listObjectsOutput.contents;
            NSLog(@"files: %@\ncount: %lu", files, (unsigned long)files.count);
            for(AWSS3Object *file in files)
            {
                NSString *key = [file key];
                NSLog(@"%@", key);
                //***HERE COMPARE STRINGS AND CHECK TO SEE IF THIS IS YOUR FILE***
            }
        }
    }] waitUntilFinished];
}
aytunch
  • 1,326
  • 1
  • 16
  • 31