Uploading a directory recursively iOS AWS S3 - ios

I have been trying to figure out how to upload a directory to S3 for quite some time now using the iOS SDK for AWS.
Currently, I have been having to .zip the directories I have been uploading. Using s3cmd on my EC2 server, I can upload directories no problem. I have also read that it is possible using other SDK's.
This is the code I have now (swift):
let folderPath = DocumentFolder.stringByAppendingString("/folderPath/folder")
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "my-bucket"
uploadRequest.serverSideEncryption = AWSS3ServerSideEncryption.AwsKms
uploadRequest.body = NSURL(fileURLWithPath: folderPath)
uploadRequest.key = "/\(folderName)/"
AWSS3TransferManager.defaultS3TransferManager().upload(uploadRequest)
However, this causes me to receive an NSInvalidArgumentException.
This is the exact error:
2016-02-26 15:11:10.672 Q-Gate[1002:417054] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSConcreteData initWithBytes:length:copy:deallocator:]: absurd length: 18446744073709551615, maximum size: 9223372036854775808 bytes'
*** First throw call stack:
(0x181e55900 0x1814c3f80 0x181e55848 0x182743ef4 0x182743e38 0x1001ccdd0 0x1001cd7fc 0x18242287c 0x182420eb4 0x182420d74 0x182420ca4 0x1824a6ee4 0x182420c30 0x101e81bb0 0x101e8b354 0x182417a88 0x181d390ac 0x18241796c 0x18241782c 0x182417658 0x181e0cefc 0x181e0c990 0x181e0a690 0x181d39680 0x1824a9434 0x182817c40 0x181abfb28 0x181abfa8c 0x181abd028)
libc++abi.dylib: terminating with uncaught exception of type NSException
If I change the path to the .zip, it uploads no problem.
Does anyone know the proper way to upload a directory to s3 using their iOS SDK?

You cannot pass a directory as body. You need to recursively list files in the directory and call - upload: for each file.
AWSS3TransferManager() is not a valid initializer for this class. The reason for the exception is
`- init` is not a valid initializer. Use `+ defaultS3TransferManager` or `+ S3TransferManagerForKey:` instead.
You need to use AWSS3TransferManager.defaultS3TransferManager() or AWSS3TransferManager(forKey: "YourKey"). Read API docs for more details.

Related

Realm , Object is already managed by another Realm

Use realm to save data.
Config the realm use defaultconfig.
then , I addOrUpdate some RLMModels
It's success.
I changed the config use
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"myname"] URLByAppendingPathExtension:#"realm"];
[RLMRealmConfiguration setDefaultConfiguration:config];
I addOrUpdate the same RLMModels
console print error:
2017-11-09 10:50:18.293801+0800 LNLibBase_Example[96588:8779968]
*** Terminating app due to uncaught exception 'RLMException',
reason: 'Object is already managed by another Realm.
Use create instead to copy it into this Realm.'
As the error message states, an object that is already managed by a Realm cannot be added to a different Realm. What you can do instead is create a copy of it in a different Realm using +createInRealm:withValue:.

Realm realmWithPath method in main bundle crashes on iOS

I do not use *.realm file in Document directory of app. I use it in main bundle directory.
When I call it:
NSString *path = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"realm"];
RLMRealm *realm = [RLMRealm realmWithPath:path];
It crashes and logs:
Terminating app due to uncaught exception 'RLMException', reason: 'open() failed: Operation not permitted'
The Realm file must be located in a directory to which you have write access in case you want to be able to update any data.
If you only intend to distribute a preset database with your app and access it read only, you must open it read only with [RLMRealm realmWithPath:readOnly:error:].

How to resolve a NULL cString crash

I'm getting a crash with the following encoding fix I'm trying to implement:
// encoding fix
NSString *correctStringTitle = [NSString stringWithCString:[[item objectForKey:#"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
cell.titleLabel.text = [correctStringTitle capitalizedString];
my crash log output states:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithCString:encoding:]: NULL cString'
thanks for any help
You seem to think that a double conversion is necessary in order to get the correct result. It isn't, nor is it possible (that's why the call to cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL). Just leave this part out and assign to correctStringTitle directly.
I had the same problem with italian accented characters while trying to get text from html and my solution to avoid the situation that cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL is ensuring that when you get the html from data you use:
[[NSString alloc] initWithData:self.responseData encoding:NSISOLatin1StringEncoding]
and not
encoding:NSUTF8StringEncoding

RaptureXML iterate:usingBlock warning and crash

I'm parsing an XML file which looks like this:
<partie numero="1">
<exercice numero="1" libelle="blabla"></exercice>
<exercice numero="2" libelle="anything"></exercice>
</partie>
I'm using the Rapture XML Library, so as explained on GitHub, I do the following :
RXMLElement *rxmlPartie = [rxmlParties child:#"partie"];
NSArray *rxmlUnExercice = [rxmlPartie children:#"exercice"];
I can print correctly the "numero" attribute from partie with this line :
NSLog(#"Partie #%#",[rxmlPartie attribute:#"numero"] );
But when I try to use the iterate method on my NSArray :
[rxmlPartie iterate:#"partie.exercice" usingBlock: ^(RXMLElement *exercice) {NSLog(#"Exercice: %# (#%#)", [exercice attribute:#"libelle"], [exercice attribute:#"numero"]);}];
I get a warning, and the the application crash, saying :
-[RXMLElement iterate:usingBlock:]: unrecognized selector sent to instance 0xc67f870
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [RXMLElement iterate:usingBlock:]: unrecognized selector sent to instance 0xc67f870'
Did I forget to import something, or do I implement the method in a bad way?
The same error happens if i try to use iterateWithRootXPath...
Thanks for your help!!
So, I found out what was my problem.
I didn't know anything about paring with Rapture XML before doing this...So I sticked to the doc (as somebody told me in school :-) ). But the problem was that a method used in the doc, this "iterate usingBlock" is not defined in the framework.
Instead of using
[rxmlPartie iterate:#"partie.exercice" usingBlock: ^(RXMLElement *exercice) {NSLog(#"Exercice: %# (#%#)", [exercice attribute:#"libelle"], [exercice attribute:#"numero"]);}];
I'm using
[rxmlPartie iterate:#"exercice" with: ^(RXMLElement *exercice) {NSLog(#"Exercice: %# (#%#)", [exercice attribute:#"libelle"], [exercice attribute:#"numero"]);}];
Which is well defined within the framework!
It made my day!!

NSRangeException while opening local .doc files on webview

I am trying to open a downloaded .doc file in UIWebview on iPad.
Basically .doc file have unicode character in both file name & file content which i am trying to open .
I got following error logs in console
WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSRangeException> *** -[NSArray objectAtIndex:]: index 0 beyond bounds for empty array
But neither webview's didFailLoadWithError or webViewDidFinishLoad delegate is called so how can i trap the error .What could be reason for it?
I've seen a few questions like this here before.
Such as:
Displaying ppt, doc, and xls in UIWebView doesn't work but pdf does
and
UIWebView does not open ms word (doc) and ms excel (xls) files
You might find some helpful hints in those questions.
Does the file open correctly when you download it on a Macintosh?
Is the mime type set correctly?
What else have you implemented in your UIWebViewDelegate?

Resources