Skip to content

Permission denied running crud_object.py #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
griffin2000 opened this issue Dec 9, 2016 · 1 comment
Closed

Permission denied running crud_object.py #707

griffin2000 opened this issue Dec 9, 2016 · 1 comment
Assignees

Comments

@griffin2000
Copy link

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)

@jerjou jerjou self-assigned this Dec 9, 2016
@jerjou
Copy link
Contributor

jerjou commented Dec 15, 2016

Ended up removing those bits of code, since they were peripheral to the sample.

@jerjou jerjou closed this as completed Dec 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants