2013-02-25 35 views
9

Sono nuovo ad AWS e lo utilizzo per l'app iOS.AWS S3 Carica l'immagine nell'app per iOS Bucket

Sto tentando di caricare immagini dalla mia app per iOS sul bucket denominato "img.haraj.com.sa". Quando carico qualsiasi immagine, non vengono mostrate nel bucket. Ma quando cambio il target in bucket chiamato "haraj", vengono caricati e mostrati nel bucket.

Ecco la politica:

{ 
    "Statement": [ 
    { 
     "Sid": "**********hidden**********", 
     "Action": [ 
     "s3:GetObject", 
     "s3:PutObject" 
     ], 
     "Effect": "Allow", 
     "Resource": [ 
     "arn:aws:s3:::haraj/*" 
     ] 
    } 
    ] 
} 

ho modificare questo per cambiare il secchio di destinazione. Ho anche creato altri bucket con il nome "img1.haraj.com.sa" e ho provato a caricare le immagini e purtroppo hanno anche fallito.

Sembra che ci sia qualche problema con i nomi dei bucket con punti (.) E senza punti. I nomi dei bucket senza punti funzionano con l'app iOS e i nomi con punti non funzionano. Non sono sicuro però. Ma sto affrontando questo problema. Non ricevo alcuna risposta di errore nel codice dell'app.

Ecco parte della mia app iOS applicazione:

- (void)postAdButtonPushed:(id)sender 
{ 
    DLog(@"Post Ad") 

    AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID withSecretKey:AWS_SECRET_KEY]; 
    s3Client.timeout = 240; 

    NSString *bucketName = [NSString stringWithFormat:@"img.haraj.com.sa"]; 
    NSString *imageName = [NSString stringWithFormat:@"testimage.jpg"]; 

    S3PutObjectRequest *objReq = [[S3PutObjectRequest alloc] initWithKey:imageName inBucket:bucketName]; 
    objReq.contentType = @"image/jpeg"; 

    UIImage *testImageToUpload = [self.imagesToUpload objectAtIndex:0]; 

    NSData *imageData = UIImageJPEGRepresentation(testImageToUpload, 0.8); 
    objReq.data = imageData; 
    objReq.delegate = self; 

    objReq.contentLength = [imageData length]; 

    [s3Client putObject:objReq]; 
} 

- (void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response 
{ 
    DLog(@"response: %@", response.description) 
} 

- (void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error 
{ 
    DLog(@"Req failed: %@", error.description) 
} 

Ho anche creato un thread sul Forum Amazon a: AWS Upload image to Bucket iOS app

Qualsiasi aiuto sarebbe apprezzato. Grazie!

+0

https://forums.aws.amazon.com/ message.jspa? messageID = 385925 – Eric

+0

Grazie a @Eric. Quella correzione è per la versione di Java. Sto usando AWS SDK in iOS. E ho integrato il framework (non il codice sorgente di sdk). Speriamo che emerga una soluzione semplice, altrimenti dovrò risolvere qualcosa con il codice sorgente. : \ –

risposta

6

Ho inviato una risposta al thread del forum here, ma per riassumere, credo che tu abbia urtato un bug nell'SDK e che sia necessario impostare esplicitamente l'endpoint S3 in cui si trova il tuo bucket.

+0

Grazie Bob! sta lavorando adesso. Spero che questo sia corretto nella prossima versione di SDK :) –

+0

Purtroppo non penso che sarà così. Inoltre, in questo caso, probabilmente andremo a gorgogliare un'eccezione, quindi a prescindere probabilmente continuerai a dover impostare l'endpoint esplicitamente per questi casi. Ciò aiuterà effettivamente le prestazioni della tua applicazione in quanto non dovrà gestire un reindirizzamento e fare una seconda richiesta. –

+0

Ok! Forse menzionare questo nella documentazione sarà più utile :) –

6

Volevo solo a pesare, ecco un frammento di codice che ho ottenuto lavorando

// #import <AWSS3/AWSS3.h> 
// #import <AWSRuntime/AWSRuntime.h> 
// then you should implement <AmazonServiceRequestDelegate> 
// import those in your .h file and 
// add the awss3 and awsruntime framework from the client 
// download from Amazon 
// myFace is the UIImage object 

    AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:@"Key_Goes_here" withSecretKey:@"Secret_Goes_Here"]; 

    NSString *imageName = [NSString stringWithFormat:@"%@.png", @"cpa"]; 

    S3PutObjectRequest *objReq = [[S3PutObjectRequest alloc] initWithKey:imageName inBucket:@"bucket_name"]; 
    objReq.contentType = @"image/png"; 
    objReq.cannedACL = [S3CannedACL publicRead]; 
    objReq.data = UIImagePNGRepresentation(myFace); 
    objReq.delegate = self; 

    [s3Client putObject:objReq]; 

Questi sono i metodi delegato:

-(void)request:(AmazonServiceRequest *)request didSendData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite { 
} 

-(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"response! %@", response); 
} 

-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response { 

} 

-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data { 
    NSLog(@"data?"); 
} 

-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error { 
    [self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Upload Error"]; 
} 
+4

hahahahahahh oh uomo. Momento di troppopieno della pila divertente. Alla ricerca di come fare qualcosa. Trova il codice che hai postato. Non ti rendi conto che hai postato. Prova a votare perché è ancora l'unica soluzione che ho trovato. – chrisallick

Problemi correlati