pythonerror-handlinggoogle-colaboratorysix

"How do I resolve the 'HTTP 403: Forbidden' error?"


I am trying to run this code in google colab but, but I'm getting "HTTP Error 403: Forbidden":

import six.moves.urllib as urllib
try:
    import urllib.request
except ImportError:
    raise ImportError('You should use Python 3.x')

if not os.path.exists('./RoadDamageDataset.tar.gz'):
    url_base = 'https://s3-ap-northeast-1.amazonaws.com/mycityreport/RoadDamageDataset.tar.gz'
    urllib.request.urlretrieve(url_base, './RoadDamageDataset.tar.gz')

    print("Download RoadDamageDataset.tar.gz Done")

else:
    print("You have RoadDamageDataset.tar.gz")

HTTPError                                 Traceback (most recent call last)

<ipython-input-16-99e6ecabe7df> in <module>()
      6 if not os.path.exists('./RoadDamageDataset.tar.gz'):
      7     url_base = 'https://s3-ap-northeast-1.amazonaws.com/mycityreport/RoadDamageDataset.tar.gz'
----> 8     urllib.request.urlretrieve(url_base, './RoadDamageDataset.tar.gz')
      9 
     10     print("Download RoadDamageDataset.tar.gz Done")

6 frames

/usr/lib/python3.6/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    648 class HTTPDefaultErrorHandler(BaseHandler):
    649     def http_error_default(self, req, fp, code, msg, hdrs):
--> 650         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    651 
    652 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

Any help will be appreciated.(link to the code in google drive: https://drive.google.com/file/d/1C-_DBepoJFL5HztTyYx9uNG22_MJB5Bo/view?usp=sharing)


Solution

  • I took the liberty of modifying your subject line: the main problem you're asking about is "How do I resolve the 'HTTP 403: Forbidden' error?". I also took the liberty of modifying your tags: your language is Python; you're importing the "six" library, and you're using Google Colaboratory.

    ANYWAY:

    https://en.wikipedia.org/wiki/HTTP_403

    HTTP 403 provides a distinct error case from HTTP 401; while HTTP 401 is returned when the client has not authenticated, and implies that a successful response may be returned following valid authentication, HTTP 403 is returned when the client is not permitted access to the resource despite providing authentication such as insufficient permissions of the authenticated account.

    Remember that the web server (here, Google Colab) runs int he context of some "user". I'm guessing that your file (RoadDamageDataset.tar.gz) might not have read permissions for the web user.

    SUGGESTION: Verify that /RoadDamageDataset.tar.gz has "read" permission.

    Also, this looks wrong:

    url_base = 'https://s3-ap-northeast-1.amazonaws.com/mycityreport/RoadDamageDataset.tar.gz'
        urllib.request.urlretrieve(url_base, './RoadDamageDataset.tar.gz')
    

    SUGGESTION: Try changing it to https://s3-ap-northeast-1.amazonaws.com/mycityreport