Micah Simmons Micah Simmons - 7 months ago 180
iOS Question

Using Firebase Storage's putFile() method is resulting in: The file “fileName” couldn’t be opened error

Here are two ways I've tried to upload the file:


getURLOfPhoto(assetURL: imagesDictionary[String(whichProfileImage)]! , completionHandler: { (responseURL) in
FIRStorage.storage().reference().putFile(responseURL as! URL)


let assets = PHAsset.fetchAssets(withALAssetURLs: [imagesDictionary[String(whichProfileImage)] as! URL], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
let imageFile = contentEditingInput?.fullSizeImageURL?

FIRStorage.storage().reference().child("test").putFile(imageFile!, metadata: nil) { (metadata, error) in
if let error = error {

I am getting this error:

Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/picture.JPG
Error Domain=NSCocoaErrorDomain Code=257 "The file “picture.JPG” couldn’t be opened because you don’t have permission to view it."
UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APPLE/picture.JPG, NSFilePath=/var/mobile/Media/DCIM/100APPLE/picture.JPG,
NSUnderlyingError=0x15da49a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

The URL seems to be being retrieved successfully and the error only occurs when the
method gets called.

Does anyone know how to fix this error or another way of uploading a file (not a Data object) to Firebase Storage?

Thanks in advance


Currently Firebase Storage is unable to use file URLs that are retrieved using the PHAsset based code I used in my question (or at least it was't able to in my experience) - even if those files are files the user took with the camera on their own iPhone. So, one solution is to re-save the file in question to a location which is accessible to the Firebase Storage API and then upload the file by passing in that location's URL in to the putFile() method.

You can use this method if you're using the imagePickerController() method:

do {
   let documentsURL = FileManager.default().urlsForDirectory(.documentDirectory,
                                                                          inDomains: .userDomainMask)[0]
   let fileURL = try documentsURL.appendingPathComponent("fileName.jpg")

   let image = info[UIImagePickerControllerOriginalImage]

   try UIImageJPEGRepresentation(image as! UIImage,1.0)?.write(to: fileURL, options: [])

                        .putFile(fileURL, metadata: nil) { (metadata, error) in
    if let error = error {
                                print("Error uploading: \(error.description)")
         catch {
            print("error is ", error)