For the sake of posterity... here is what Bard says.

*Prompt*: In golang, develop code to find the size of a google doc

*Response*:

To find the size of a Google Doc in Golang, you can use the following steps:

   1. Import the necessary packages: 

import ( "context" "fmt" "io/ioutil" "net/http" 
"github.com/google/go-auth/oauth2" 
"github.com/googleapis/google-api-go/docs/v1" ) 

   1. Create a new OAuth2 client and obtain an access token: 

ctx := context.Background() config := oauth2.Config{ ClientID: 
"YOUR_CLIENT_ID", ClientSecret: "YOUR_CLIENT_SECRET", Scopes: 
[]string{"https://www.googleapis.com/auth/drive.readonly"}, RedirectURL: 
"http://localhost:8080/oauth2callback";, } token, err := 
config.Exchange(ctx, "YOUR_AUTHORIZATION_CODE") if err != nil { 
log.Fatalf("Unable to exchange code: %v", err) } 

   1. Create a new Google Docs client: 

client := docs.NewService(http.Client{ Transport: &oauth2.Transport{ 
Source: token, }, }) 

   1. Get the size of the Google Doc: 

docID := "YOUR_DOCUMENT_ID" req, err := client.Documents.Get(docID).Do(ctx) 
if err != nil { log.Fatalf("Unable to get document: %v", err) } size := 
req.ContentLength 

   1. Print the size of the Google Doc: 

fmt.Printf("The size of the Google Doc is %d bytes\n", size) 

Here is a complete example of a Golang program to find the size of a Google 
Doc:
package main import ( "context" "fmt" "io/ioutil" "net/http" 
"github.com/google/go-auth/oauth2" 
"github.com/googleapis/google-api-go/docs/v1" ) func main() { ctx := 
context.Background() config := oauth2.Config{ ClientID: "YOUR_CLIENT_ID", 
ClientSecret: "YOUR_CLIENT_SECRET", Scopes: 
[]string{"https://www.googleapis.com/auth/drive.readonly"}, RedirectURL: 
"http://localhost:8080/oauth2callback";, } token, err := 
config.Exchange(ctx, "YOUR_AUTHORIZATION_CODE") if err != nil { 
log.Fatalf("Unable to exchange code: %v", err) } client := 
docs.NewService(http.Client{ Transport: &oauth2.Transport{ Source: token, 
}, }) docID := "YOUR_DOCUMENT_ID" req, err := 
client.Documents.Get(docID).Do(ctx) if err != nil { log.Fatalf("Unable to 
get document: %v", err) } size := req.ContentLength fmt.Printf("The size of 
the Google Doc is %d bytes\n", size) } 

To use this code, you will need to replace the following values:

   - YOUR_CLIENT_ID: Your Google Cloud Platform client ID. 
   - YOUR_CLIENT_SECRET: Your Google Cloud Platform client secret. 
   - YOUR_AUTHORIZATION_CODE: An authorization code that you can obtain by 
   following the OAuth2 flow. 
   - YOUR_DOCUMENT_ID: The ID of the Google Doc that you want to find the 
   size of. 

Once you have replaced these values, you can compile and run the code. The 
program will print the size of the Google Doc to the console.
On Thursday, October 5, 2023 at 10:12:25 PM UTC-4 Mandolyte wrote:

> Yeah, I need someone who knows something about how gdrive does things. 
> Thanks for the help!
>
> On Thursday, October 5, 2023 at 7:43:08 PM UTC-4 Kurtis Rader wrote:
>
>> Did you notice the `ls` command reports the file size is 170? The same as 
>> your program? Also, `ls` does not indicate the file is a symlink so the 
>> fact that ioutil.ReadDir presumably behaves the same as os.ReadDir (i.e., 
>> both use os.Lstat()) that isn't an issue. So for whatever reason the Google 
>> Drive FS is either not reporting the actual size of the file or the file is 
>> really just 170 bytes in length.
>>
>> On Thu, Oct 5, 2023 at 4:31 PM Mandolyte <ceci...@gmail.com> wrote:
>>
>>> Here is what I see:
>>> $ ls -l Household\ Cleaning.gdoc 
>>> -rw-r----- 1 cecil chronos-access 170 Apr 22 15:30 'Household 
>>> Cleaning.gdoc'
>>> $ 
>>>
>>> I'll update the code to remove the deprecated and see if that is any 
>>> different.
>>> On Thursday, October 5, 2023 at 6:06:50 PM UTC-4 Kurtis Rader wrote:
>>>
>>>> Note that ioutil.ReadDir() has been deprecated since Go 1.16. Which is 
>>>> a very long time. Its documentation is ambiguous but given the behavior 
>>>> you're reporting I would guess it is using the equivalent of os.Lstat() 
>>>> and 
>>>> the "files" you're dealing with are symlinks. Note that os.ReadDir() 
>>>> (which 
>>>> replaced ioutil.ReadDir) explicitly states that is what it does:
>>>>
>>>> Readdir reads the contents of the directory associated with file and 
>>>> returns a slice of up to n FileInfo values, as would be returned by Lstat, 
>>>> in directory order. Subsequent calls on the same file will yield further 
>>>> FileInfos.
>>>>
>>>> On Thu, Oct 5, 2023 at 1:35 PM Mandolyte <ceci...@gmail.com> wrote:
>>>>
>>>>> Thanks for responding! The entire program may be found here: 
>>>>> https://go.dev/play/p/4kLaeWeQG0Z
>>>>>
>>>>> The size comes from os.FileInfo, which is:
>>>>>
>>>>> type FileInfo interface {
>>>>> Name() string <https://pkg.go.dev/builtin#string> // base name of the 
>>>>> file Size() int64 <https://pkg.go.dev/builtin#int64> // length in 
>>>>> bytes for regular files; system-dependent for others Mode() FileMode 
>>>>> <https://pkg.go.dev/io/fs#FileMode> // file mode bits ModTime() time 
>>>>> <https://pkg.go.dev/time>.Time <https://pkg.go.dev/time#Time> // 
>>>>> modification time IsDir() bool <https://pkg.go.dev/builtin#bool> // 
>>>>> abbreviation for Mode().IsDir() Sys() any 
>>>>> <https://pkg.go.dev/builtin#any> // underlying data source (can 
>>>>> return nil) }
>>>>>
>>>>> These are collected around line 112 and following.
>>>>> On Thursday, October 5, 2023 at 1:12:48 PM UTC-4 Kurtis Rader wrote:
>>>>>
>>>>>> It would help if you showed us the relevant code. My first guess is 
>>>>>> those files are symlinks. Or perhaps this is simply what Google Drive 
>>>>>> reports when a file is stat()'d. What does `ls -l` show?
>>>>>>
>>>>>> On Thu, Oct 5, 2023 at 8:31 AM Mandolyte <ceci...@gmail.com> wrote:
>>>>>>
>>>>>>> I have a program that given a starting folder will walk the 
>>>>>>> directory structure and report file sizes and other things.
>>>>>>>
>>>>>>> I recently tried to run this code on a mounted Google Drive on 
>>>>>>> ChromeOS Linux. All the google docs report a size 170 bytes. Snippet:
>>>>>>>
>>>>>>> [image: Screenshot 2023-10-05 11.29.44 AM.png]
>>>>>>>
>>>>>>> Any pointers appreciated!
>>>>>>>
>>>>>>> -- 
>>>>>>> You received this message because you are subscribed to the Google 
>>>>>>> Groups "golang-nuts" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>>> send an email to golang-nuts...@googlegroups.com.
>>>>>>> To view this discussion on the web visit 
>>>>>>> https://groups.google.com/d/msgid/golang-nuts/8faf6106-2fdd-49e3-a193-e8f41605fbffn%40googlegroups.com
>>>>>>>  
>>>>>>> <https://groups.google.com/d/msgid/golang-nuts/8faf6106-2fdd-49e3-a193-e8f41605fbffn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>>
>>>>>>
>>>>>>
>>>>>> -- 
>>>>>> Kurtis Rader
>>>>>> Caretaker of the exceptional canines Junior and Hank
>>>>>>
>>>>> -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "golang-nuts" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to golang-nuts...@googlegroups.com.
>>>>>
>>>> To view this discussion on the web visit 
>>>>> https://groups.google.com/d/msgid/golang-nuts/f00d389d-e55a-4afe-8754-c5acd063c320n%40googlegroups.com
>>>>>  
>>>>> <https://groups.google.com/d/msgid/golang-nuts/f00d389d-e55a-4afe-8754-c5acd063c320n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>
>>>>
>>>> -- 
>>>> Kurtis Rader
>>>> Caretaker of the exceptional canines Junior and Hank
>>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to golang-nuts...@googlegroups.com.
>>>
>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/golang-nuts/69891205-4545-4242-86b5-55485db21276n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/golang-nuts/69891205-4545-4242-86b5-55485db21276n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> -- 
>> Kurtis Rader
>> Caretaker of the exceptional canines Junior and Hank
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/60211970-4390-43fc-b757-d762428f3fc8n%40googlegroups.com.

Reply via email to