Skip to content

Permission denied running crud_object.py  #707

@griffin2000

Description

@griffin2000

In which file did you encounter the issue?

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/api/crud_object.py

Describe the issue

As written the crud_object.py file main function will fail with this error (on windows at least):

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\owner\\AppData\\Local\\Temp\\tmp3preaezj'


Traceback (most recent call last):
  File "crud_object.py", line 150, in <module>
    main(args.bucket, args.filename, args.reader, args.owner)
  File "crud_object.py", line 47, in main
    if not filecmp.cmp(filename, tmpfile.name):
  File "E:\Python\Python35\lib\filecmp.py", line 62, in cmp
    outcome = _do_cmp(f1, f2)
  File "E:\Python\Python35\lib\filecmp.py", line 75, in _do_cmp
    with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:

The issue is that NamedTemporaryFile keeps the file handle open so that when filecmp attempts to open it, it will fail with a permission denied error. The correct way to do this is shown below:

    print('Fetching object..')
    with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tmpfile:
        get_object(bucket, filename, out_file=tmpfile)
        tmpfile.seek(0)
        tmpName = tmpfile.name
    if not filecmp.cmp(filename, tmpName):
        raise Exception('Downloaded file != uploaded object')
    print('Deleting object..')
    os.remove(tmpName)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions