You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: django_gcp/storage/blob_utils.py
+7-62Lines changed: 7 additions & 62 deletions
Original file line number
Diff line number
Diff line change
@@ -1,74 +1,14 @@
1
1
fromdatetimeimporttimedelta
2
2
fromos.pathimportsplit
3
3
4
-
fromgoogle.cloud.storage.blobimportBlob
5
-
6
-
7
-
defupload_blob(
8
-
instance,
9
-
field_name,
10
-
local_path,
11
-
destination_path=None,
12
-
attributes=None,
13
-
allow_overwrite=False,
14
-
existing_path=None,
15
-
):
16
-
"""Upload a file to the cloud store, using the instance and field name to determine the store details
17
-
18
-
You might use this utility to upload fixtue files for integration tests, as part of
19
-
a migration, or as part of a function creating local files. The field's own logic for generating paths
20
-
is used by default, although this can be overridden.
21
-
22
-
Returns the field value so you can use this to construct instances directly:
23
-
24
-
# Directly set the instance field without processing the blob ingress
25
-
with override_settings(GCP_STORAGE_OVERRIDE_BLOBFIELD_VALUE=True):
26
-
instance.field_name = upload_blob(...)
27
-
28
-
:param django.db.Model instance: An instance of a django Model which has a BlobField
29
-
:param str field_name: The name of the BlobField attribute on the instance
30
-
:param str local_path: The path to the file to upload
31
-
:param str destination_path: The path to upload the file to. If None, the remote path will be generated
32
-
from the BlobField. If setting this value, take care to override the value of the field on the instance
33
-
so it's path matches; this is not updated for you.
34
-
:param dict attributes: A dictionary of attributes to set on the blob eg content type
35
-
:param bool allow_overwrite: If true, allows existing blobs at the path to be overwritten. If destination_path is not given, this is provided to the get_destination_path callback (and may be overridden by that callback per its specification)
36
-
:param str existing_path: If destination_path is None, this is provided to the get_destination_path callback to simulate behaviour where there is an existing path
:param django.db.Model instance: An instance of a django Model which has a BlobField
87
27
:param str field_name: The name of the BlobField attribute on the instance
28
+
:param bool reload: Default True. If it is not essential to have up-to-date information from the store, speed up the call to get_blob using call with reload_blob=False
f"'update_attributes' argument in {self.__class__.__name__} must be None, or a callable function that updates attributes to be set on ingressed blobs.",
"""Upload a file to the cloud store, using the instance and field name to determine the store details
29
+
30
+
You might use this utility to upload fixtue files for integration tests, as part of
31
+
a migration, or as part of a function creating local files. The field's own logic for generating paths
32
+
is used by default, although this can be overridden.
33
+
34
+
Returns the field value so you can use this to construct instances directly:
35
+
36
+
# Directly set the instance field without processing the blob ingress
37
+
with override_settings(GCP_STORAGE_OVERRIDE_BLOBFIELD_VALUE=True):
38
+
instance.field_name = upload_blob(...)
39
+
40
+
:param django.db.Model instance: An instance of a django Model which has a BlobField
41
+
:param str field_name: The name of the BlobField attribute on the instance
42
+
:param str local_path: The path to the file to upload
43
+
:param str destination_path: The path to upload the file to. If None, the remote path will be generated
44
+
from the BlobField. If setting this value, take care to override the value of the field on the instance
45
+
so it's path matches; this is not updated for you.
46
+
:param dict attributes: A dictionary of attributes to set on the blob eg content type
47
+
:param bool allow_overwrite: If true, allows existing blobs at the path to be overwritten. If destination_path is not given, this is provided to the get_destination_path callback (and may be overridden by that callback per its specification)
48
+
:param str existing_path: If destination_path is None, this is provided to the get_destination_path callback to simulate behaviour where there is an existing path
0 commit comments