Downloading metadata from Firebase Storage - swift

unable to get access to my metadata on the new Firebase. Able to get download of article and have them display.
storageRef.child(article).metadataWithCompletion { (metadata, error) in
if error != nil{
print("error getting metadata")
}else{
let metadata1 = FIRStorageMetadata()
let nameMeta = metadata1.downloadURLs
print("nameMeta is \(nameMeta)")
}
output display:
nameMeta is nil

You shouldn't create a new metatada instance, you should use the one already provided in the closure.
else {
let downloadUrl = metadata.downloadUrl()
print(downloadUrl)
}

Related

Trouble Uploading Multiple Images to Firebase Storage

I have an array of image data(in bytes), and I want to upload them to storage and get the download URL for all of them. But after uploading lots of pictures, there is only one file appears in the storage under the reference. I do get the download URLs, but only the last one works, the rest all say access denied.
I suspect it is because I am putting all the pictures under the same storage reference and the picture substitutes every time. But what can I do to upload multiple images?
My upload code:
var picturesURL: [String] = [] // I want to put the download URL in this array
if self.imageButton.isSelected{
picturesURL = []
for photoData in self.pictures{ // pictures is the array that stores all the photo data
self.storage.child("images/file.png").putData(photoData, metadata: nil) { _, error in
guard error == nil else{
print("Failed to upload")
return
}
self.storage.child("images/file.png").downloadURL (completion: { url, error in
guard let url = url, error == nil else{
print("problems getting url")
return
}
let urlString = url.absoluteString
picturesURL.append(urlString)
print(urlString)
//UserDefaults.standard.set(urlString, forKey: "url")
})
}
}

Chat: Value of type 'StorageMetadata' has no member 'downloadURLs'

I'm trying to implement a chat to firebase but get this error: Value of type 'StorageMetadata' has no member 'downloadURLs' from this code:
var fileUrl: String!
func CreateNewRoom(user: User, caption: String, data: NSData) {
let filePath = "\(user.uid)/\(Int(NSDate.timeIntervalSinceReferenceDate))"
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.child(filePath).putData(data as Data, metadata: metaData) { (metadata, error) in
if let error = error {
print("Error uploading: \(error as NSError)")
return
}
self.fileUrl = metadata!.downloadURLs![0].absoluteString
if let user = Auth.auth().currentUser {
let idRoom = self.BASE_REF.child("rooms").childByAutoId()
idRoom.setValue(["caption": caption, "thumbnailUrlFromStorage": self.storageRef.child(metadata!.path!).description, "fileUrl": self.fileUrl])
}
}
}
StorageMetadata doesn't have a property called downloadURLs, as you can seem from the API documentation. That's been deprecated a long time ago and removed from the API. You're probably looking at an old example or tutorial.
If you need to generate a download URL for a file in Cloud Storage, use downloadURL() to fetch one.

Upload Image to Amazon S3 Using Swift3

After looking around at different threads such as these (Swift - AWS S3 Upload Image from Photo Library and download it), and (Upload image AWS S3 bucket in swift), I got pretty close to getting an image upload to work but can't figure out what I'm doing wrong?
I get the following error below in my console.
Error: Error Domain=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=1 "(null)" UserInfo={Server=AmazonS3, Transfer-Encoding=Identity, Connection=close, Content-Type=application/xml, Date=Tue, 13 Dec 2016 05:58:32 GMT, x-amz-request-id=2A76DF0FE33476C5, x-amz-id-2=QWZgOETbWQfddlqKmm0w3Z9HFGM2x1DWnrFjukiajTsIXfbSt9W0orTkoZeNXH/bI1xfc3mxI4Q=, x-amz-bucket-region=us-west-1}
My code is below:
let myIdentityPoolId = "us-west-2:dca2beb4-etcetcetc...."
let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.usWest2, identityPoolId: myIdentityPoolId)
let configuration = AWSServiceConfiguration(region: AWSRegionType.usWest2, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
I then call to upload my image with a function I made below
func uploadImage(filename:String){
print("AWS Upload Image Attempt...")
//defining bucket and upload file name
let S3BucketName: String = "distribution-tech-mobile"
let filepath = "\(AppDelegate.appDelegate.applicationDocumentsDirectory())/\(filename)"
let imageURL = URL(fileURLWithPath: filepath)
let S3UploadKeyName = filename //TODO: Change this later
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = S3BucketName
uploadRequest?.key = filename
uploadRequest?.contentType = "image/jpeg"
uploadRequest?.body = imageURL
uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.awsKms
uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
DispatchQueue.main.async(execute: {
self.amountUploaded = totalBytesSent // To show the updating data status in label.
self.fileSize = totalBytesExpectedToSend
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
self.uploadCompletionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
if ((error) != nil){
print("Failed with error")
print("Error: \(error!)");
}
else{
print("Sucess")
}
})
}
let transferUtility = AWSS3TransferUtility.default()
let expression = AWSS3TransferUtilityUploadExpression()
transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continue({ (task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let exception = task.exception {
print("Exception: \(exception.description)")
}
if let _ = task.result {
print("Upload Starting!")
}
return nil;
})
}
I get the console message "Upload Starting" and then a message "Failed with error" (which comes from my completion handler), followed by the error I assume from Amazon.
Any thoughts on what I'm doing wrong?
Okay I found the answer, but I have a different problem now that I'll post in another question regarding showing upload progress.
The answer was my bucket was created in the incorrect region. I created my credentials in Oregon, which is Us-West-2, and I created the bucket in Northern California by accident the first time. This apparently created the error.

Error saving CKRecord and CKShare in CKModifyRecordsOperation

I'm trying to save a CKRecord (already created and saved to the database) and a new CKShare to it but I keep getting this error: Optional("Failed to modify some records"). Here is my code:
let csc = UICloudSharingController { controller, preparationCompletionHandler in
let share = CKShare(rootRecord: user)
share[CKShareTitleKey] = "My Share" as CKRecordValue
share.publicPermission = .readWrite
let mro = CKModifyRecordsOperation(recordsToSave: [user, share], recordIDsToDelete: nil)
mro.timeoutIntervalForRequest = 10
mro.timeoutIntervalForResource = 10
mro.modifyRecordsCompletionBlock = { records, recordIDs, error in
if error != nil {
print("ERROR IN MODIFY RECORDS COMPLETION BLOCK\n")
print(error?.localizedDescription)
}
preparationCompletionHandler(share,CKContainer.default(), error)
}
privateData.add(mro)
}
csc.availablePermissions = [.allowPrivate,.allowReadWrite]
self.present(csc, animated:true)
}
The problem is in this method: modifyRecordsCompletionBlock. Can somebody explain me why this is happening?
Thanks in advance!
I got it
All you have to do is create a private custom CKZone and save your CKRecord and CKShare in it, you cannot use the default zone cloud kit gives to you!
To leave a shared record, you have to delete the CKShare record from the shared database otherwise you'll get errors.

Images not caching with Firebase url but caching with other

I have little problem. I got caching to work with the following URL:
let URL = NSURL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!
But can't get it to work like this with this URL:
FIRStorage.storage().reference().child("\(productImageref!).png").downloadURLWithCompletion({(url, error)in
if error != nil{
print(error)
return
}else{
cell.snusProductImageView.kf_setImageWithURL(url , placeholderImage: nil,
optionsInfo: [.Transition(ImageTransition.Fade(1))],
progressBlock: { receivedSize, totalSize in
print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
},
completionHandler: { image, error, cacheType, imageURL in
print("\(indexPath.row + 1): Finished")
})
}
})
What I am doing wrong here? Can you point me to the right direction? For caching I use the 3rd party library "KingFisher"
Edit: Firebase guy Mike McDonald's quote
"The Github one has Cache-Control: max-age=300 while Firebase Storage
doesn't have cache control set by default (you can set it when you
upload the file, or change it by updating metadata), so I assume
that's why KingFisher isn't caching it."
KingFisher owner quotes.
Like Firebase guy Mike McDonald said I directly set the cache-control when uploading the image to the Firebase storage. Then it will be cached correctly when loading it later.
(Configuration: Swift3, Firebase Storage 3.11.0)
let storage = FIRStorage.storage()
let storageRef = storage.reference(forURL: yourFirebaseStorageReferenceURL)
let imageRef = storageRef.child("profileImages/\(imageUUID).jpg")
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
// THIS is the important part
metadata.cacheControl = "public,max-age=2592000"
let uploadTask = imageRef.put(imageData, metadata: metadata) { (metadata, error) in
if let error = error
{
completion(.failure(error as NSError))
}
guard let metadata = metadata, let downloadURL = metadata.downloadURL()?.absoluteString else {
// Handle upload error
return
}
// Handle successful upload
}
When I download the image, the cache-control is set
The way I got them caching was that I had to write all image url-s into JSON like this:
.setValue(url!.absoluteString)
And then read those as url-s