0

I have images stored in Google Cloud Storage Bucket

Here is example url: https://storage.cloud.google.com/avatars-pl8/MenuCategoryFile-2019-10-15-13-27-55-de57e7770c12ccc4aaf7aba612428b63.jpg

And I am trying to load it with URLSession.dataTask

let urlRequest = URLRequest(url: url)


        DispatchQueue(label: "image-loader", qos: .background).async {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: urlRequest) { data, response, error in

                guard (response as? HTTPURLResponse)?.statusCode == 200 else {

I am getting correct response status code 200 but data is empty 0 bytes!

When I've checked response.url then there is

po response?.url
▿ Optional<URL>
 ▿ some : https://accounts.google.com/ServiceLogin?service=cds&passive=1209600&continue=https://storage.cloud.google.com/avatars-pl8/MenuCategoryFile-2019-10-15-13-33-22-98e605c9c33581c6d1f8624b57c2c4f2.jpg&followup=https://storage.cloud.google.com/avatars-pl8/MenuCategoryFile-2019-10-15-13-33-22-98e605c9c33581c6d1f8624b57c2c4f2.jpg
   - _url : https://accounts.google.com/ServiceLogin?service=cds&passive=1209600&continue=https://storage.cloud.google.com/avatars-pl8/MenuCategoryFile-2019-10-15-13-33-22-98e605c9c33581c6d1f8624b57c2c4f2.jpg&followup=https://storage.cloud.google.com/avatars-pl8/MenuCategoryFile-2019-10-15-13-33-22-98e605c9c33581c6d1f8624b57c2c4f2.jpg

And accessing via web browser I have

https://00e9e64bac41b0499c2718c49a22532bc6616fd8750c6c1205-apidata.googleusercontent.com/download/storage/v1/b/avatars-pl8/o/MenuCategoryFile-2019-10-15-13-27-55-de57e7770c12ccc4aaf7aba612428b63.jpg?qk=AD5uMEtiqq-AyvI_5iXnitsf4-zdD-4GZm0vLfmVKSU8hg_s3kLsoCIRxk2tMnrADommifmFlmPLVEJ5kucWX6_6S8VK0dhhxkfuprVqbR75q9orDRqw1jRdO4xyxqlWH12Rp2njQIoo6TxCbYCzBpGXltuD-siHUCpNn3zT0jPt7XCrj1JoddT1-215Zjppv_NibT2-wRAMw3I0LlJ5GxRRgaAwQ9p9LDvhkpCIVo6Ikuokj84l9lSchtmMvZtu48sKLX8K22ix2S3OeWEC9p3p4FWUFQlLtqDIpKlmhZVV8AK6vZ4eL4Vu8Rzg4Vi8I5IHT9J_MFzSJ2LfUL_wHiBjouv863MrC-cWuRQgGwhQDb7KNMc6cVrLu8XsR_2s8aOBmlj9Jm3fvnknLz2jg-TXWXnbTk_1iXO1O1OtNkUdRybsG9fBv3GJ_3l_9qQpy-ntfe5Smsb58kAoY9vuicbPSfN8HifER2VxwCp-eVwkjoFtr-XbnwJyMYUkpkOOQ1C7HaV8hQsjkonzU7xeyoAA6_x1iU1konP7mD0lLN3znZsNoYxIdanlNPoc7EyIZUR0ZmD_Y3NcIE32WNl3X8UAyOe36_mA1XcZyWZxrmo0d4f4skg4WXskzioLKgX2XcY9DrPiK3_Yw9sPvW5H4XPUtBuZgWRChBI4SnZ01KwCEVNpsgHtn0DoFjZ_6zXofZeZ38jRwj42KsykQtVLSUt3OLVQbTyZjV_7pEBj5dLK_5X1loLSaPiyR4el4Z6cH_7EiJ2nU1yI9h51pXMq0fZeYe3EuUVwOCy4PQCWLpHvEMcqQ3DkmG_cLrQ-MtKROYdD4tIaGGwXXKdsM4fXDYsZrEpXbAPzTY72--r4phBsmtjs0YPoCg8

I think there is some problem with Authorization of access and redirection? This images are made public on Google Bucket

Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • I've seen this [post](https://stackoverflow.com/questions/46696624/swift-urlsession-shared-datatask-says-status-code-304-200) where the user was getting a 200 status code while the expected was 304. Could you please try setting the cachePolicy to .reloadIgnoringCacheData? `request.cachePolicy = .reloadIgnoringCacheData` . Let me know if the status code is still the same or it changes. – ericcco Oct 17 '19 at 10:56
  • 1
    I've found it but it was related to URL storage.cloud.google.com is not appropriate for accessing this images. There is another. Maybe I add answer with correct url. The above redirects to Google Account login page – Michał Ziobro Oct 17 '19 at 11:02
  • I understand that you were able to resolve the issue. It would be great if you can share the solution as an answer in order to let other users know in case there is someone else facing the same issue. – ericcco Oct 17 '19 at 11:06
  • @MichałZiobro Good point. – MyounghoonKim Nov 18 '19 at 10:02
  • @MichałZiobro https://stackoverflow.com/users/1115332/mkatleast3 had you resolved this issue, I am also getting the same. I need help if you can – Naresh Jan 27 '20 at 10:21

0 Answers0