drive_v3 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/examples/README.md +70 -76
- data/examples/drive_service_batch +7 -1
- data/examples/file_create +127 -8
- data/examples/file_delete +6 -1
- data/examples/file_download_content +6 -1
- data/examples/file_export_spreadsheet +22 -2
- data/examples/file_recover_from_trash +10 -1
- data/examples/file_search +10 -1
- data/examples/file_send_to_trash +6 -1
- data/examples/file_upload_content +6 -1
- data/examples/permission_create +12 -1
- data/examples/permission_delete +7 -1
- data/examples/permission_list +13 -1
- data/examples/permission_update +14 -1
- data/lib/drive_v3/version.rb +1 -1
- metadata +3 -5
- data/examples/file_create_folder +0 -46
- data/examples/file_create_spreadsheet +0 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb569b1ffaa8be00b9f846624af85f93e8d09739fa9d0ea2c10c0a7854fd4952
|
4
|
+
data.tar.gz: 75a30ae59c88404277a8e0c904887d1f7799e1e62afdade7ba8268840c5b941b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecad9653b07257b4895e277d9f5558e71a68db4ae68c0fc193404b525695f137381263314c304e8231e70e86b355afbb9fb900c2072286a49948b37fb1929c42
|
7
|
+
data.tar.gz: 86113a5a2ce266cbbca8e9bc4005ee2c8c3e9c02d8dd2ab416edb580f1e7e935de25225a8b2e33059838b3f9ad74e9e7de0a7e0c921aa1c89992d9fd6d123b63
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,15 @@ Changes for each release are listed in this file.
|
|
4
4
|
|
5
5
|
This project adheres to [Semantic Versioning](https://semver.org/) for its releases.
|
6
6
|
|
7
|
+
## v0.2.1 (2023-12-05)
|
8
|
+
|
9
|
+
[Full Changelog](https://github.com/main-branch/drive_v3/compare/v0.2.0..v0.2.1)
|
10
|
+
|
11
|
+
Changes since v0.2.0:
|
12
|
+
|
13
|
+
* f1b82bd Update examples to include the supports_all_drives parameter (#6)
|
14
|
+
* b26f1fa Release v0.2.0 (#5)
|
15
|
+
|
7
16
|
## v0.2.0 (2023-12-04)
|
8
17
|
|
9
18
|
[Full Changelog](https://github.com/main-branch/drive_v3/compare/v0.1.0..v0.2.0)
|
data/examples/README.md
CHANGED
@@ -4,53 +4,52 @@ Annotated examples written in Ruby.
|
|
4
4
|
|
5
5
|
Checked (✅︎) topics are completed. Topics without a check still need to be added.
|
6
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
|
-
* [Manage labels](#manage-labels)
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
* [Search by label or field](#search-by-label-or-field)
|
7
|
+
* [Google Drive Examples](#google-drive-examples)
|
8
|
+
* [Getting started](#getting-started)
|
9
|
+
* [✅︎ Creating a Google API service account](#︎-creating-a-google-api-service-account)
|
10
|
+
* [✅︎ Creating a DriveService instance](#︎-creating-a-driveservice-instance)
|
11
|
+
* [✅︎ Batching DriveService requests](#︎-batching-driveservice-requests)
|
12
|
+
* [Files and folders](#files-and-folders)
|
13
|
+
* [✅︎ Search for files](#︎-search-for-files)
|
14
|
+
* [✅︎ Get file](#︎-get-file)
|
15
|
+
* [✅︎ Create a file](#︎-create-a-file)
|
16
|
+
* [✅︎ Upload file data](#︎-upload-file-data)
|
17
|
+
* [✅︎ Download file data](#︎-download-file-data)
|
18
|
+
* [✅︎ Export file](#︎-export-file)
|
19
|
+
* [✅︎ Send file to trash](#︎-send-file-to-trash)
|
20
|
+
* [✅︎ Recover file from trash](#︎-recover-file-from-trash)
|
21
|
+
* [✅︎ Delete a file](#︎-delete-a-file)
|
22
|
+
* [Share files, folders, and drives](#share-files-folders-and-drives)
|
23
|
+
* [✅︎ Create permission](#︎-create-permission)
|
24
|
+
* [✅︎ List permissions](#︎-list-permissions)
|
25
|
+
* [✅︎ Update permission](#︎-update-permission)
|
26
|
+
* [✅︎ Delete permission](#︎-delete-permission)
|
27
|
+
* [Shortcuts](#shortcuts)
|
28
|
+
* [Create a shortcut to a file](#create-a-shortcut-to-a-file)
|
29
|
+
* [Search for shortcuts](#search-for-shortcuts)
|
30
|
+
* [Other](#other)
|
31
|
+
* [File revisions](#file-revisions)
|
32
|
+
* [Store application-specific data](#store-application-specific-data)
|
33
|
+
* [Manage file metadata](#manage-file-metadata)
|
34
|
+
* [Manage comments and replies](#manage-comments-and-replies)
|
35
|
+
* [Add custom file properties](#add-custom-file-properties)
|
36
|
+
* [Create a shortcut to a Drive file](#create-a-shortcut-to-a-drive-file)
|
37
|
+
* [Create a shortcut to app content](#create-a-shortcut-to-app-content)
|
38
|
+
* [Protect file content from modification](#protect-file-content-from-modification)
|
39
|
+
* [Access link-shared files using resource keys](#access-link-shared-files-using-resource-keys)
|
40
|
+
* [Handle changes](#handle-changes)
|
41
|
+
* [Identify which change log to track](#identify-which-change-log-to-track)
|
42
|
+
* [Track changes for users and shared drives](#track-changes-for-users-and-shared-drives)
|
43
|
+
* [Retrieve changes](#retrieve-changes)
|
44
|
+
* [Receive notifications for resource changes](#receive-notifications-for-resource-changes)
|
45
|
+
* [Manage labels](#manage-labels)
|
46
|
+
* [Manage labels](#manage-labels-1)
|
47
|
+
* [Set label field](#set-label-field)
|
48
|
+
* [Unset label field](#unset-label-field)
|
49
|
+
* [Remove label](#remove-label)
|
50
|
+
* [List labels from file](#list-labels-from-file)
|
51
|
+
* [Return specific labels from a file](#return-specific-labels-from-a-file)
|
52
|
+
* [Search by label or field](#search-by-label-or-field)
|
54
53
|
|
55
54
|
## Getting started
|
56
55
|
|
@@ -109,42 +108,37 @@ shows how to get a
|
|
109
108
|
[File](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/classes.rb)
|
110
109
|
from the drive and controlling which fields are returned.
|
111
110
|
|
112
|
-
### ✅︎ Create
|
111
|
+
### ✅︎ Create a file
|
113
112
|
|
114
|
-
|
115
|
-
|
116
|
-
|
113
|
+
Using the `drive_service.create_file` with no parameters creates an empty data file
|
114
|
+
with the following attributes:
|
115
|
+
* `kind` is set to "drive.file",
|
116
|
+
* `id` is set to a unique id
|
117
|
+
* `name` is set to "Untitled"
|
118
|
+
* `mime_type` of application/octet-stream.
|
117
119
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
[examples/file_create](https://github.com/main-branch/drive_v3/blob/main/examples/file_create)
|
123
|
-
shows how to create an empty data file and write data to it.
|
124
|
-
|
125
|
-
### ✅︎ Create a folder
|
120
|
+
The `mime_type` parameter defines the type of file to create or to create a folder.
|
121
|
+
See [Google Workspace & Google Drive supported MIME types](https://developers.google.com/drive/api/guides/mime-types)
|
122
|
+
for a list of supported MIME types.
|
126
123
|
|
127
|
-
|
128
|
-
|
124
|
+
Google Apps files (document, spreadsheet, or presentation) may be created by specifying
|
125
|
+
their specific `mime-type`:
|
126
|
+
* **Google Docs**: application/vnd.google-apps.document
|
127
|
+
* **Google Sheets**: application/vnd.google-apps.spreadsheet
|
128
|
+
* **Google Slides**: application/vnd.google-apps.presentation
|
129
|
+
* See [Google Workspace & Google Drive supported MIME types](https://developers.google.com/drive/api/guides/mime-types)
|
130
|
+
for a complete list.
|
129
131
|
|
130
132
|
See [Create and populate folders](https://developers.google.com/drive/api/guides/folder#create-folder)
|
131
|
-
for more
|
132
|
-
|
133
|
-
### ✅︎ Create a spreadsheet, document, or presentation
|
133
|
+
for more about creating a folder. A folder is a file with a `mime_type` of
|
134
|
+
"application/vnd.google-apps.folder".
|
134
135
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
See [Create a spreadsheet](https://developers.google.com/sheets/api/guides/create#work_with_folders)
|
139
|
-
for more information.
|
140
|
-
|
141
|
-
Use the following mime-types for Google apps:
|
142
|
-
* Spreadsheet: **application/vnd.google-apps.spreadsheet**
|
143
|
-
* Document: **application/vnd.google-apps.document**
|
144
|
-
* Presentation: **application/vnd.google-apps.presentation**
|
136
|
+
Use the `:parents` parameter to give the id of the folder that should contain the
|
137
|
+
file. Omitting this parameter or passing an empty array will place the new file
|
138
|
+
in the user's `My Drive` root folder.
|
145
139
|
|
146
|
-
|
147
|
-
|
140
|
+
[examples/file_create](https://github.com/main-branch/drive_v3/blob/main/examples/file_create)
|
141
|
+
shows how to create a file specifying name, mime-type, parent, and initial data.
|
148
142
|
|
149
143
|
### ✅︎ Upload file data
|
150
144
|
|
@@ -51,6 +51,12 @@ permission = Google::Apis::DriveV3::Permission.new(
|
|
51
51
|
role: 'writer'
|
52
52
|
)
|
53
53
|
|
54
|
+
# Indicate that this application supports files in shared drives. An error will
|
55
|
+
# result if this is false (or omitted) AND you are trying to update the permissions
|
56
|
+
# on a file in a shared drive.
|
57
|
+
#
|
58
|
+
supports_all_drives = true
|
59
|
+
|
54
60
|
begin
|
55
61
|
results = [].tap do |request_results|
|
56
62
|
drive_service.batch do |service|
|
@@ -59,7 +65,7 @@ begin
|
|
59
65
|
#
|
60
66
|
# Collect the result (or error if there is one) in the block passed to
|
61
67
|
# `create_permission`
|
62
|
-
service.create_permission(spreadsheet_id, permission) do |res, err|
|
68
|
+
service.create_permission(spreadsheet_id, permission, supports_all_drives:) do |res, err|
|
63
69
|
request_results << (res || err.message)
|
64
70
|
end
|
65
71
|
end
|
data/examples/file_create
CHANGED
@@ -1,35 +1,143 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
+
# Create a file or folder in a Google Drive
|
5
|
+
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
# Parse the command line
|
9
|
+
#
|
10
|
+
# @example Create a file in a Google Drive
|
11
|
+
# file-create \
|
12
|
+
# [NAME] \
|
13
|
+
# [--parent ID] \
|
14
|
+
# [--mime-type MIME_TYPE] \
|
15
|
+
# [--content FILE_NAME [--content-type MIME_TYPE]]
|
16
|
+
#
|
17
|
+
# @example Create a new empty file with default title and mimetype in the user's My Drive
|
18
|
+
# file-create
|
19
|
+
#
|
20
|
+
# @example Create a file name 'My Data File' in the user's My Drive
|
21
|
+
# file-create 'My Data File'
|
22
|
+
#
|
23
|
+
# @example Create a file in a specific folder
|
24
|
+
# file-create 'My Data File' --parent '1V6RS_7_YgJmLDH-BDw3dpD8Np60Oi9ET'
|
25
|
+
#
|
26
|
+
# @example Create a specific kind of file (data file)
|
27
|
+
# file-create 'My Data File' --mime-type 'text/plain'
|
28
|
+
#
|
29
|
+
# @example Create a specific kind of file (spreadsheet)
|
30
|
+
# file-create 'My Data File' --mime-type 'application/vnd.google-apps.spreadsheet'
|
31
|
+
#
|
32
|
+
# @example Create a specific kind of file (document)
|
33
|
+
# file-create 'My Data File' --mime-type 'application/vnd.google-apps.document'
|
34
|
+
#
|
35
|
+
# @example Create a folder
|
36
|
+
# file-create 'My New Folder' --mime-type 'application/vnd.google-apps.folder'
|
37
|
+
#
|
38
|
+
# @example Create a file and load it with data from a file
|
39
|
+
# file-create 'My Data File' --content 'data.csv' --content-type 'text/csv'
|
40
|
+
#
|
41
|
+
# @example For a anyone
|
42
|
+
# permission-create FILE_ID \
|
43
|
+
# --type anyone \
|
44
|
+
# --role ROLE \
|
45
|
+
# [--expiration TIME] \
|
46
|
+
# [{--allow-file-discovery | --no-allow-file-discovery}]
|
47
|
+
#
|
48
|
+
class ParseCommandLine
|
49
|
+
def initialize(argv)
|
50
|
+
@argv = argv.dup
|
51
|
+
default_options
|
52
|
+
parser.parse!(@argv)
|
53
|
+
@name = @argv.shift
|
54
|
+
validate(@argv)
|
55
|
+
end
|
56
|
+
|
57
|
+
attr_reader :name, :parent_id, :mime_type, :content, :content_type
|
58
|
+
|
59
|
+
def banner = <<~BANNER
|
60
|
+
Usage:
|
61
|
+
|
62
|
+
#{$PROGRAM_NAME} [NAME] [options]
|
63
|
+
|
64
|
+
Creates a file in a Google Drive and displays the JSON properties
|
65
|
+
of that file.
|
66
|
+
|
67
|
+
WARNING: Omitting NAME will create a file named 'Untitled'.
|
68
|
+
|
69
|
+
Options:
|
70
|
+
BANNER
|
71
|
+
|
72
|
+
def parser
|
73
|
+
@parser ||= OptionParser.new(banner) do |opts|
|
74
|
+
option_definitions.each { |option_definition| opts.on(*option_definition) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def help
|
79
|
+
puts parser
|
80
|
+
exit
|
81
|
+
end
|
82
|
+
|
83
|
+
def option_definitions
|
84
|
+
[
|
85
|
+
help_definition,
|
86
|
+
parent_definition, mime_type_definition,
|
87
|
+
content_definition, content_type_definition
|
88
|
+
].freeze
|
89
|
+
end
|
90
|
+
|
91
|
+
def help_definition = ['-h', '--help', '', ->(_value) { help }]
|
92
|
+
def parent_definition = ['--parent=ID', ->(value) { @parent_id = value }]
|
93
|
+
def mime_type_definition = ['--mime-type=MIME_TYPE', ->(value) { @mime_type = value }]
|
94
|
+
def content_definition = ['--content=FILE_NAME', ->(value) { @content = value }]
|
95
|
+
def content_type_definition = ['--content_type=MIME_TYPE', ->(value) { @content_type = value }]
|
96
|
+
|
97
|
+
def default_options; end
|
98
|
+
|
99
|
+
def validate(argv)
|
100
|
+
raise "Extra command line arguments: #{argv}" unless argv.empty?
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
4
104
|
# Create a data file
|
5
105
|
#
|
6
106
|
# When creating a data file, upload_source should be an IO object that
|
7
107
|
# responds to `read` and `size` (e.g. StringIO) or a string that names
|
8
108
|
# an existing file to upload.
|
9
109
|
|
110
|
+
options = ParseCommandLine.new(ARGV)
|
111
|
+
|
10
112
|
require 'drive_v3'
|
11
113
|
require 'json'
|
12
114
|
|
13
115
|
drive_service = DriveV3.drive_service
|
14
116
|
|
15
|
-
# If name is not specified as part of a create request, the file name
|
16
|
-
# 'Untitled'.
|
17
|
-
name =
|
117
|
+
# If name is not specified as part of a create request (or it is nil), the file name
|
118
|
+
# is named 'Untitled'.
|
119
|
+
name = options.name
|
18
120
|
|
19
121
|
# If parents not specified as part of a create request or is an empty array, the
|
20
122
|
# file is placed directly in the user's My Drive folder
|
21
123
|
#
|
22
124
|
# parents = ['id1', 'id2']
|
23
|
-
parents = [
|
125
|
+
parents = []
|
126
|
+
parents << options.parent_id if options.parent_id
|
24
127
|
|
25
128
|
# The MIME type of the file. Drive will attempt to automatically detect an
|
26
129
|
# appropriate value from uploaded content if no value is provided. The value
|
27
130
|
# cannot be changed unless a new revision is uploaded.
|
28
131
|
#
|
132
|
+
# See
|
133
|
+
# [Google Workspace & Google Drive supported MIME types](https://developers.google.com/drive/api/guides/mime-types)
|
134
|
+
# for a list of supported MIME types.
|
135
|
+
#
|
29
136
|
# The default MIME type is 'text/plain'
|
30
137
|
#
|
138
|
+
#
|
31
139
|
# mime_type = nil
|
32
|
-
mime_type =
|
140
|
+
mime_type = options.mime_type
|
33
141
|
|
34
142
|
file_metadata = { name:, parents:, mime_type: }
|
35
143
|
|
@@ -47,7 +155,7 @@ fields = '*'
|
|
47
155
|
# data file.
|
48
156
|
#
|
49
157
|
# upload_source = StringIO.new("This is my letter to the World\nThat never wrote to Me")
|
50
|
-
upload_source =
|
158
|
+
upload_source = options.content
|
51
159
|
|
52
160
|
# Content type indicates the MIME type of the upload_source. If not specified,
|
53
161
|
# the content type will be determined by calling MIME::Types.of with the
|
@@ -56,10 +164,21 @@ upload_source = StringIO.new("1,2,3\n4,5,6\n7,8,9")
|
|
56
164
|
# content_type = 'text/plain'
|
57
165
|
# CSV content type
|
58
166
|
# content_type = nil
|
59
|
-
content_type =
|
167
|
+
content_type = options.content_type
|
168
|
+
|
169
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
170
|
+
# this is false (or omitted) AND `create_file` tries to create a file in a shared drive.
|
171
|
+
#
|
172
|
+
supports_all_drives = true
|
60
173
|
|
61
174
|
begin
|
62
|
-
file = drive_service.create_file(
|
175
|
+
file = drive_service.create_file(
|
176
|
+
file_metadata,
|
177
|
+
fields:,
|
178
|
+
upload_source:,
|
179
|
+
content_type:,
|
180
|
+
supports_all_drives:
|
181
|
+
)
|
63
182
|
puts JSON.pretty_generate(file.to_h)
|
64
183
|
rescue StandardError => e
|
65
184
|
puts "An error occurred: #{e.message}"
|
data/examples/file_delete
CHANGED
@@ -14,10 +14,15 @@ drive_service = DriveV3.drive_service
|
|
14
14
|
file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
|
15
15
|
raise 'Missing file_id' unless file_id
|
16
16
|
|
17
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
18
|
+
# this is false (or omitted) AND `delete_file` tries to create a spreadsheet in a shared drive.
|
19
|
+
#
|
20
|
+
supports_all_drives = true
|
21
|
+
|
17
22
|
begin
|
18
23
|
print 'Delete file...'
|
19
24
|
# Does not return anything
|
20
|
-
drive_service.delete_file(file_id)
|
25
|
+
drive_service.delete_file(file_id, supports_all_drives:)
|
21
26
|
puts 'Success'
|
22
27
|
rescue StandardError => e
|
23
28
|
puts "An error occurred: #{e.message}"
|
@@ -17,10 +17,15 @@ raise 'Missing file_id' unless file_id
|
|
17
17
|
# to.
|
18
18
|
download_dest = StringIO.new
|
19
19
|
|
20
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
21
|
+
# this is false (or omitted) AND `get_file` tries to get a file from s shared drive.
|
22
|
+
#
|
23
|
+
supports_all_drives = true
|
24
|
+
|
20
25
|
# Read content from file
|
21
26
|
#
|
22
27
|
begin
|
23
|
-
drive_service.get_file(file_id, download_dest:)
|
28
|
+
drive_service.get_file(file_id, download_dest:, supports_all_drives:)
|
24
29
|
puts JSON.pretty_generate(download_dest.string)
|
25
30
|
rescue StandardError => e
|
26
31
|
puts "An error occurred: #{e.message}"
|
@@ -14,6 +14,15 @@ require 'json'
|
|
14
14
|
|
15
15
|
drive_service = DriveV3.drive_service
|
16
16
|
|
17
|
+
# Indicate that this application supports files in shared drives. An error will
|
18
|
+
# result if this is false (or omitted) AND you try to do something with a file or
|
19
|
+
# folder in a shared drive.
|
20
|
+
#
|
21
|
+
# Note that the `export_file` method does not require this parameter to export
|
22
|
+
# a file from a shared drive.
|
23
|
+
#
|
24
|
+
supports_all_drives = true
|
25
|
+
|
17
26
|
# First create a spreadsheet, populating with some data.
|
18
27
|
# Then get the file id of the spreadsheet.
|
19
28
|
|
@@ -25,7 +34,13 @@ fields = 'id, name'
|
|
25
34
|
upload_source = StringIO.new("1,2,3\n4,5,6\n7,8,9")
|
26
35
|
content_type = 'text/csv'
|
27
36
|
begin
|
28
|
-
file = drive_service.create_file(
|
37
|
+
file = drive_service.create_file(
|
38
|
+
file_metadata,
|
39
|
+
fields:,
|
40
|
+
upload_source:,
|
41
|
+
content_type:,
|
42
|
+
supports_all_drives:
|
43
|
+
)
|
29
44
|
puts JSON.pretty_generate(file.to_h)
|
30
45
|
rescue StandardError => e
|
31
46
|
puts "An error occurred: #{e.message}"
|
@@ -39,7 +54,12 @@ mime_type = 'application/pdf'
|
|
39
54
|
download_dest = "#{file.name}.pdf"
|
40
55
|
|
41
56
|
begin
|
42
|
-
drive_service.export_file(
|
57
|
+
drive_service.export_file(
|
58
|
+
file_id,
|
59
|
+
mime_type,
|
60
|
+
download_dest:,
|
61
|
+
supports_all_drives:
|
62
|
+
)
|
43
63
|
rescue StandardError => e
|
44
64
|
puts "An error occurred: #{e.message}"
|
45
65
|
exit 1
|
@@ -14,10 +14,19 @@ drive_service = DriveV3.drive_service
|
|
14
14
|
file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
|
15
15
|
raise 'Missing file_id' unless file_id
|
16
16
|
|
17
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
18
|
+
# this is false (or omitted) AND you are trying to update a file in a shared drive.
|
19
|
+
#
|
20
|
+
supports_all_drives = true
|
21
|
+
|
17
22
|
begin
|
18
23
|
print 'Recovering trashed file...'
|
19
24
|
file_object = { trashed: false }
|
20
|
-
file = drive_service.update_file(
|
25
|
+
file = drive_service.update_file(
|
26
|
+
file_id,
|
27
|
+
file_object,
|
28
|
+
supports_all_drives:
|
29
|
+
)
|
21
30
|
puts "update_file result:\n#{file.to_h.pretty_inspect}"
|
22
31
|
rescue StandardError => e
|
23
32
|
puts "An error occurred: #{e.message}"
|
data/examples/file_search
CHANGED
@@ -71,9 +71,17 @@ fields = '*'
|
|
71
71
|
#
|
72
72
|
include_items_from_all_drives = true
|
73
73
|
|
74
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
75
|
+
# this is false (or omitted) AND `list_files` returns a file from a shared drive.
|
76
|
+
#
|
77
|
+
supports_all_drives = true
|
78
|
+
|
74
79
|
loop do
|
75
80
|
file_list = @drive_service.list_files(
|
76
|
-
q:,
|
81
|
+
q:,
|
82
|
+
fields:,
|
83
|
+
include_items_from_all_drives:,
|
84
|
+
supports_all_drives:,
|
77
85
|
page_token:, page_size:
|
78
86
|
)
|
79
87
|
files += file_list.files.map(&:to_h)
|
@@ -84,3 +92,4 @@ end
|
|
84
92
|
# Print as JSON with links to each file and its parents
|
85
93
|
|
86
94
|
puts JSON.pretty_generate(files)
|
95
|
+
# puts files.count
|
data/examples/file_send_to_trash
CHANGED
@@ -14,10 +14,15 @@ drive_service = DriveV3.drive_service
|
|
14
14
|
file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
|
15
15
|
raise 'Missing file_id' unless file_id
|
16
16
|
|
17
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
18
|
+
# this is false (or omitted) AND you are trying to update a file in a shared drive.
|
19
|
+
#
|
20
|
+
supports_all_drives = true
|
21
|
+
|
17
22
|
begin
|
18
23
|
print 'Trash file...'
|
19
24
|
file_object = { trashed: true }
|
20
|
-
file = drive_service.update_file(file_id, file_object)
|
25
|
+
file = drive_service.update_file(file_id, file_object, supports_all_drives:)
|
21
26
|
puts "update_file result:\n#{file.to_h.pretty_inspect}"
|
22
27
|
rescue StandardError => e
|
23
28
|
puts "An error occurred: #{e.message}"
|
@@ -15,10 +15,15 @@ raise 'Missing file_id' unless file_id
|
|
15
15
|
#
|
16
16
|
upload_source = StringIO.new('This is the content that replaced the original content')
|
17
17
|
|
18
|
+
# Indicate that this application supports files in shared drives. An error will result if
|
19
|
+
# this is false (or omitted) AND you are trying to update a file in a shared drive.
|
20
|
+
#
|
21
|
+
supports_all_drives = true
|
22
|
+
|
18
23
|
# Replace the content of an existing file
|
19
24
|
#
|
20
25
|
begin
|
21
|
-
file = drive_service.update_file(file_id, upload_source:)
|
26
|
+
file = drive_service.update_file(file_id, upload_source:, supports_all_drives:)
|
22
27
|
puts JSON.pretty_generate(file.to_h)
|
23
28
|
rescue StandardError => e
|
24
29
|
puts "An error occurred: #{e.message}"
|
data/examples/permission_create
CHANGED
@@ -155,8 +155,19 @@ drive_service = DriveV3.drive_service
|
|
155
155
|
options = PermissionCreateCli.new(ARGV)
|
156
156
|
fields = '*'
|
157
157
|
|
158
|
+
# Indicate that this application supports files in shared drives. An error will
|
159
|
+
# result if this is false (or omitted) AND you are trying to update the permissions
|
160
|
+
# on a file in a shared drive.
|
161
|
+
#
|
162
|
+
supports_all_drives = true
|
163
|
+
|
158
164
|
begin
|
159
|
-
result = drive_service.create_permission(
|
165
|
+
result = drive_service.create_permission(
|
166
|
+
options.file_id,
|
167
|
+
options.permission,
|
168
|
+
fields:,
|
169
|
+
supports_all_drives:
|
170
|
+
)
|
160
171
|
# The result.id is 'anyoneWithLink' if type=anyone
|
161
172
|
# The result.id is of the existing permission if it already exists
|
162
173
|
puts JSON.pretty_generate(result.to_h)
|
data/examples/permission_delete
CHANGED
@@ -15,8 +15,14 @@ raise "Extra arguments: #{ARGV}" unless ARGV.empty?
|
|
15
15
|
|
16
16
|
drive_service = DriveV3.drive_service
|
17
17
|
|
18
|
+
# Indicate that this application supports files in shared drives. An error will
|
19
|
+
# result if this is false (or omitted) AND you are trying to delete the permissions
|
20
|
+
# on a file in a shared drive.
|
21
|
+
#
|
22
|
+
supports_all_drives = true
|
23
|
+
|
18
24
|
begin
|
19
|
-
drive_service.delete_permission(file_id, permission_id)
|
25
|
+
drive_service.delete_permission(file_id, permission_id, supports_all_drives:)
|
20
26
|
rescue StandardError => e
|
21
27
|
puts "An error occurred: #{e.message}"
|
22
28
|
end
|
data/examples/permission_list
CHANGED
@@ -39,8 +39,20 @@ page_token = nil
|
|
39
39
|
# fields = 'nextPageToken, files(id, name, parents, web_view_link)'
|
40
40
|
fields = '*'
|
41
41
|
|
42
|
+
# Indicate that this application supports files in shared drives. An error will
|
43
|
+
# result if this is false (or omitted) AND you are trying to list the permissions
|
44
|
+
# on a file in a shared drive.
|
45
|
+
#
|
46
|
+
supports_all_drives = true
|
47
|
+
|
42
48
|
loop do
|
43
|
-
permission_list = drive_service.list_permissions(
|
49
|
+
permission_list = drive_service.list_permissions(
|
50
|
+
file_id,
|
51
|
+
fields:,
|
52
|
+
page_token:,
|
53
|
+
page_size:,
|
54
|
+
supports_all_drives:
|
55
|
+
)
|
44
56
|
permissions += permission_list.permissions.map(&:to_h)
|
45
57
|
# Stop looping if there are no more pages
|
46
58
|
break unless (page_token = permission_list.next_page_token)
|
data/examples/permission_update
CHANGED
@@ -126,8 +126,21 @@ permission = options.permission
|
|
126
126
|
remove_expiration = options.remove_expiration
|
127
127
|
fields = '*'
|
128
128
|
|
129
|
+
# Indicate that this application supports files in shared drives. An error will
|
130
|
+
# result if this is false (or omitted) AND you are trying to update permissions
|
131
|
+
# on a file in a shared drive.
|
132
|
+
#
|
133
|
+
supports_all_drives = true
|
134
|
+
|
129
135
|
begin
|
130
|
-
result = drive_service.update_permission(
|
136
|
+
result = drive_service.update_permission(
|
137
|
+
file_id,
|
138
|
+
permission_id,
|
139
|
+
permission,
|
140
|
+
remove_expiration:,
|
141
|
+
fields:,
|
142
|
+
supports_all_drives:
|
143
|
+
)
|
131
144
|
puts "Permission updated: #{result.to_h.pretty_inspect}"
|
132
145
|
rescue StandardError => e
|
133
146
|
puts "An error occurred: #{e.message}"
|
data/lib/drive_v3/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: drive_v3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Couball
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler-audit
|
@@ -259,8 +259,6 @@ files:
|
|
259
259
|
- examples/README.md
|
260
260
|
- examples/drive_service_batch
|
261
261
|
- examples/file_create
|
262
|
-
- examples/file_create_folder
|
263
|
-
- examples/file_create_spreadsheet
|
264
262
|
- examples/file_delete
|
265
263
|
- examples/file_download_content
|
266
264
|
- examples/file_export_spreadsheet
|
@@ -300,7 +298,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
300
298
|
- !ruby/object:Gem::Version
|
301
299
|
version: '0'
|
302
300
|
requirements: []
|
303
|
-
rubygems_version: 3.4.
|
301
|
+
rubygems_version: 3.4.10
|
304
302
|
signing_key:
|
305
303
|
specification_version: 4
|
306
304
|
summary: Unofficial helpers and extensions for the Google Drive V3 API
|
data/examples/file_create_folder
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
# Create a folder
|
5
|
-
#
|
6
|
-
# When creating a Google Drive folder, you create a file with a MIME type of
|
7
|
-
# 'application/vnd.google-apps.folder' and a name. You can also specify the
|
8
|
-
# folder's parent folder(s) and other metadata.
|
9
|
-
|
10
|
-
require 'drive_v3'
|
11
|
-
require 'json'
|
12
|
-
|
13
|
-
drive_service = DriveV3.drive_service
|
14
|
-
|
15
|
-
# If name is not specified as part of a create request, the file name is named
|
16
|
-
# 'Untitled'.
|
17
|
-
name = 'My New Folder'
|
18
|
-
|
19
|
-
# If parents not specified as part of a create request or is an empty array, the
|
20
|
-
# folder is placed directly in the user's My Drive folder
|
21
|
-
#
|
22
|
-
# parents = ['id1', 'id2']
|
23
|
-
parents = []
|
24
|
-
|
25
|
-
# The MIME type of the file. Drive will attempt to automatically detect an
|
26
|
-
# appropriate value from uploaded content if no value is provided. The value
|
27
|
-
# cannot be changed unless a new revision is uploaded.
|
28
|
-
#
|
29
|
-
# A folder MIME type is 'application/vnd.google-apps.folder'
|
30
|
-
#
|
31
|
-
mime_type = 'application/vnd.google-apps.folder'
|
32
|
-
|
33
|
-
# If fields is not specified, the following default fields are returned:
|
34
|
-
# id, name, kind, and mime_type. '*' can by used to return all fields.
|
35
|
-
# See https://developers.google.com/drive/api/v3/reference/files#resource
|
36
|
-
#
|
37
|
-
fields = '*'
|
38
|
-
|
39
|
-
file_metadata = { name:, parents:, mime_type: }
|
40
|
-
|
41
|
-
begin
|
42
|
-
file = drive_service.create_file(file_metadata, fields:)
|
43
|
-
puts JSON.pretty_generate(file.to_h)
|
44
|
-
rescue StandardError => e
|
45
|
-
puts "An error occurred: #{e.message}"
|
46
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
# Create a Google Sheets spreadsheet in Google Drive
|
5
|
-
#
|
6
|
-
# When creating a spreadsheet, you create a file with a MIME type of
|
7
|
-
# 'application/vnd.google-apps.spreadsheet' and a name. You can also specify the
|
8
|
-
# spreadsheet's parent folder and other metadata.
|
9
|
-
#
|
10
|
-
# The file_create method allows loading an Excel spreadheet or a sheet of data
|
11
|
-
# from CSV or TSV data. Specify the data to load using `upload_source` and
|
12
|
-
# `content_type` parameters. If `upload_source` is not specified, the file will
|
13
|
-
# be created as an empty spreadsheet.
|
14
|
-
|
15
|
-
require 'drive_v3'
|
16
|
-
require 'json'
|
17
|
-
|
18
|
-
drive_service = DriveV3.drive_service
|
19
|
-
|
20
|
-
# If name is not specified as part of a create request, the file name is named
|
21
|
-
# 'Untitled'.
|
22
|
-
name = 'My spreadsheet file'
|
23
|
-
|
24
|
-
# If parents not specified as part of a create request or is an empty array, the
|
25
|
-
# file is placed directly in the user's My Drive folder
|
26
|
-
#
|
27
|
-
# parents = ['id1', 'id2']
|
28
|
-
parents = []
|
29
|
-
|
30
|
-
# The MIME type of the file. Drive will attempt to automatically detect an
|
31
|
-
# appropriate value from uploaded content if no value is provided. The value
|
32
|
-
# cannot be changed unless a new revision is uploaded.
|
33
|
-
#
|
34
|
-
# The default MIME type is 'text/plain'
|
35
|
-
#
|
36
|
-
# mime_type = nil
|
37
|
-
mime_type = 'application/vnd.google-apps.spreadsheet'
|
38
|
-
|
39
|
-
file_metadata = { name:, parents:, mime_type: }
|
40
|
-
|
41
|
-
# If fields is not specified, the following default fields are returned:
|
42
|
-
# id, name, kind, and mime_type. '*' can by used to return all fields.
|
43
|
-
# See https://developers.google.com/drive/api/v3/reference/files#resource
|
44
|
-
#
|
45
|
-
fields = '*'
|
46
|
-
|
47
|
-
# When creating the data file, upload_source should be an IO object that
|
48
|
-
# responds to `read` and `size` (e.g. StringIO) or a string that names
|
49
|
-
# an existing file to upload.
|
50
|
-
#
|
51
|
-
# If upload_source is not specified, the file will be created as an empty
|
52
|
-
# data file.
|
53
|
-
#
|
54
|
-
upload_source = StringIO.new("1,2,3\n4,5,6\n7,8,9")
|
55
|
-
|
56
|
-
# Content type indicates the MIME type of `upload_source`. If not specified,
|
57
|
-
# the content type will be determined by calling MIME::Types.of with the
|
58
|
-
# upload_source's filename.
|
59
|
-
#
|
60
|
-
# content_type = 'text/plain'
|
61
|
-
# CSV content type
|
62
|
-
# content_type = nil
|
63
|
-
content_type = 'text/csv'
|
64
|
-
|
65
|
-
begin
|
66
|
-
file = drive_service.create_file(file_metadata, fields:, upload_source:, content_type:)
|
67
|
-
puts JSON.pretty_generate(file.to_h)
|
68
|
-
rescue StandardError => e
|
69
|
-
puts "An error occurred: #{e.message}"
|
70
|
-
end
|