FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-docs-samples/storage/api/crud_object.py at master · git-connected/python-docs-samples · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
git-connected
/
python-docs-samples
Public
forked from
smooth80/python-docs-samples
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-docs-samples
/
storage
/
api
/
crud_object.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
139 lines (105 loc) · 4.44 KB
Breadcrumbs
python-docs-samples
/
storage
/
api
/
crud_object.py
Copy path
File metadata and controls
139 lines (105 loc) · 4.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# Copyright (C) 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Application for uploading an object using the Cloud Storage API.
This sample is used on this page:
https://cloud.google.com/storage/docs/json_api/v1/json-api-python-samples
For more information, see the README.md under /storage.
"""
import
argparse
import
json
import
tempfile
import
googleapiclient
.
discovery
import
googleapiclient
.
http
def
main
(
bucket
,
filename
,
readers
=
[],
owners
=
[]):
print
(
'Uploading object..'
)
resp
=
upload_object
(
bucket
,
filename
,
readers
,
owners
)
print
(
json
.
dumps
(
resp
,
indent
=
2
))
print
(
'Fetching object..'
)
with
tempfile
.
TemporaryFile
(
mode
=
'w+b'
)
as
tmpfile
:
get_object
(
bucket
,
filename
,
out_file
=
tmpfile
)
print
(
'Deleting object..'
)
resp
=
delete_object
(
bucket
,
filename
)
if
resp
:
print
(
json
.
dumps
(
resp
,
indent
=
2
))
print
(
'Done'
)
def
create_service
():
# Construct the service object for interacting with the Cloud Storage API -
# the 'storage' service, at version 'v1'.
# You can browse other available api services and versions here:
# http://g.co/dv/api-client-library/python/apis/
return
googleapiclient
.
discovery
.
build
(
'storage'
,
'v1'
)
def
upload_object
(
bucket
,
filename
,
readers
,
owners
):
service
=
create_service
()
# This is the request body as specified:
# http://g.co/cloud/storage/docs/json_api/v1/objects/insert#request
body
=
{
'name'
:
filename
,
}
# If specified, create the access control objects and add them to the
# request body
if
readers
or
owners
:
body
[
'acl'
]
=
[]
for
r
in
readers
:
body
[
'acl'
].
append
({
'entity'
:
'user-%s'
%
r
,
'role'
:
'READER'
,
'email'
:
r
})
for
o
in
owners
:
body
[
'acl'
].
append
({
'entity'
:
'user-%s'
%
o
,
'role'
:
'OWNER'
,
'email'
:
o
})
# Now insert them into the specified bucket as a media insertion.
# http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#insert
with
open
(
filename
,
'rb'
)
as
f
:
req
=
service
.
objects
().
insert
(
bucket
=
bucket
,
body
=
body
,
# You can also just set media_body=filename, but for the sake of
# demonstration, pass in the more generic file handle, which could
# very well be a StringIO or similar.
media_body
=
googleapiclient
.
http
.
MediaIoBaseUpload
(
f
,
'application/octet-stream'
))
resp
=
req
.
execute
()
return
resp
def
get_object
(
bucket
,
filename
,
out_file
):
service
=
create_service
()
# Use get_media instead of get to get the actual contents of the object.
# http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#get_media
req
=
service
.
objects
().
get_media
(
bucket
=
bucket
,
object
=
filename
)
downloader
=
googleapiclient
.
http
.
MediaIoBaseDownload
(
out_file
,
req
)
done
=
False
while
done
is
False
:
status
,
done
=
downloader
.
next_chunk
()
print
(
"Download {}%."
.
format
(
int
(
status
.
progress
()
*
100
)))
return
out_file
def
delete_object
(
bucket
,
filename
):
service
=
create_service
()
req
=
service
.
objects
().
delete
(
bucket
=
bucket
,
object
=
filename
)
resp
=
req
.
execute
()
return
resp
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
)
parser
.
add_argument
(
'filename'
,
help
=
'The name of the file to upload'
)
parser
.
add_argument
(
'bucket'
,
help
=
'Your Cloud Storage bucket.'
)
parser
.
add_argument
(
'--reader'
,
action
=
'append'
,
default
=
[],
help
=
'Your Cloud Storage bucket.'
)
parser
.
add_argument
(
'--owner'
,
action
=
'append'
,
default
=
[],
help
=
'Your Cloud Storage bucket.'
)
args
=
parser
.
parse_args
()
main
(
args
.
bucket
,
args
.
filename
,
args
.
reader
,
args
.
owner
)
Back
|
FazBrowse Home
|
New Git URL