使用Go将文件和文件夹上传到Box账户。

huangapple go评论115阅读模式
英文:

Uploading the files and folders using Go to the box account

问题

以下是您提供的代码的翻译:

对于下面的程序,我得到了以下错误。如果有人能帮助我修复错误,将非常有帮助。提前感谢。

  1. func upload() {
  2. fmt.Println("dfxfgcghvjbjhiiiiiiiiiiiiiiiiiii")
  3. apiUrl := "https://upload.box.com/"
  4. resource := "api/2.0/files/content"
  5. data := url.Values{}
  6. data.Add("access_token", accessobj.Access_token)
  7. authbear := "Bearer "
  8. authbear += accessobj.Access_token
  9. u, _ := url.ParseRequestURI(apiUrl)
  10. u.Path = resource
  11. urlStr := fmt.Sprintf("%v", u)
  12. client := &http.Client{}
  13. fmt.Println(urlStr)
  14. f, err := ioutil.ReadFile("C:\\Users\\vembu\\Desktop\\hi.txt")
  15. ioutil.WriteFile("hi.txt", f, 0x777)
  16. r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f))
  17. r.Header.Add("Authorization", "Bearer "+accessobj.Access_token)
  18. r.Header.Add("attributes",
  19. "{\"name\":\"hi.txt\",\"parent\":{\"id\":\"3098791209\"}}")
  20. r.Header.Add("file", "hi.txt")
  21. fmt.Println(r)
  22. if err != nil {
  23. fmt.Println("error......:", err)
  24. }
  25. resp, err1 := client.Do(r)
  26. if err1 != nil {
  27. fmt.Println("error:", err1)
  28. }
  29. fmt.Println("uploading")
  30. fmt.Println(resp)
  31. re, err := ioutil.ReadAll(resp.Body)
  32. if err != nil {
  33. fmt.Println("errorrrrr:", err)
  34. }
  35. fmt.Println(string(re))
  36. }

我得到了以下错误:

我遇到了 &{405 Method Not Allowed 405 HTTP/1.1 1 1 map[Allow:[GET, OPTIONS, HEAD] Content-Type:[text/html;charset=UTF-8] Content-Length:[0] Date:[Thu, 12 Mar 2015 13:07:32 GMT] Age:[0] Connection:[keep-alive] Server:[ATS]] 0xc08200b8c0 0 [] false map[] 0xc08201f2b0 0xc082060980}

英文:

For the program below I get the below error. It will be helpful if anyone helps me to fix my errors. Thanks in advance.

  1. func upload() {
  2. fmt.Println("dfxfgcghvjbjhiiiiiiiiiiiiiiiiiii")
  3. apiUrl := "https://upload.box.com/"
  4. resource := "api/2.0/files/content"
  5. data := url.Values{}
  6. data.Add("access_token", accessobj.Access_token)
  7. authbear := "Bearer "
  8. authbear += accessobj.Access_token
  9. u, _ := url.ParseRequestURI(apiUrl)
  10. u.Path = resource
  11. urlStr := fmt.Sprintf("%v", u)
  12. client := &http.Client{}
  13. fmt.Println(urlStr)
  14. f, err := ioutil.ReadFile("C:\\Users\\vembu\\Desktop\\hi.txt")
  15. ioutil.WriteFile("hi.txt", f, 0x777)
  16. r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f))
  17. r.Header.Add("Authorization", "Bearer "+accessobj.Access_token)
  18. r.Header.Add("attributes",
  19. "{\"name\":\"hi.txt\",\"parent\":{\"id\":\"3098791209\"}}")
  20. r.Header.Add("file", "hi.txt")
  21. fmt.Println(r)
  22. if err != nil {
  23. fmt.Println("error......:", err)
  24. }
  25. resp, err1 := client.Do(r)
  26. if err1 != nil {
  27. fmt.Println("error:", err1)
  28. }
  29. fmt.Println("uploading")
  30. fmt.Println(resp)
  31. re, err := ioutil.ReadAll(resp.Body)
  32. if err != nil {
  33. fmt.Println("errorrrrr:", err)
  34. }
  35. fmt.Println(string(re))
  36. }

I get the following error:

  1. I face &{405 Method Not Allowed 405 HTTP/1.1 1 1
  2. map[Allow:[GET, OPTIONS, HEAD] Content-Type:[text/html;charset=UTF-8]
  3. Content-Length:[0] Date:[Thu, 12 Mar 2015 13:07:32 GMT] Age:[0]
  4. Connection:[keep-alive]
  5. Server:[ATS]] 0xc08200b8c0 0 [] false map[] 0xc08201f2b0 0xc082060980}

答案1

得分: 2

问题在于你尝试进行一个HTTP POST请求:

  1. r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f))

但是服务器不允许/支持这样做,如响应错误中所述(只允许GETOPTIONSHEAD方法):

> Method Not Allowed 405 HTTP/1.1 1 1 map[Allow:[GET, OPTIONS, HEAD]

根据box.com API,要使用POST上传文件,你需要使用多部分表单上传请求。

你可以使用multipart包创建一个带有文件的多部分请求。

以下是一个示例代码(不完整/未经测试):

  1. buf := &bytes.Buffer{}
  2. mw := multipart.NewWriter(buf)
  3. defer mw.Close()
  4. f, err := os.Open("C:\\Users\\vembu\\Desktop\\hi.txt")
  5. if err != nil {
  6. // 处理错误
  7. }
  8. defer f.Close()
  9. ff, err := mw.CreateFormFile("name", "hi.txt")
  10. if err != nil {
  11. // 处理错误
  12. }
  13. if _, err = io.Copy(ff, f); err != nil {
  14. // 处理错误
  15. }
  16. // TODO: 在URL中包含其他字段/参数
  17. r, err := http.NewRequest("POST", "https://upload.box.com/api/2.0/files/content", buf)
  18. if err != nil {
  19. // 处理错误
  20. }
  21. r.Header.Set("Content-Type", mw.FormDataContentType())
  22. // TODO: 添加其他头字段
  23. // 发起调用:上传文件
  24. client := &http.Client{}
  25. resp, err := client.Do(r)
  26. if err != nil {
  27. // 处理错误
  28. }
  29. if resp.StatusCode != http.StatusOK {
  30. fmt.Printf("错误:%v", resp.Status)
  31. }
英文:

The problem is that you try to do an HTTP POST request:

  1. r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f))

But the server does not allow/support this as stated in the response error (only GET, OPTIONS and HEAD methods are allowed):

> Method Not Allowed 405 HTTP/1.1 1 1 map[Allow:[GET, OPTIONS, HEAD]

According to the box.com API to upload a file using POST you need to use a multi-part form upload request.

You can use the multipart package to create a multipart request with a file.

Here is an example how to do it (incomplete/untested code):

  1. buf := &bytes.Buffer{}
  2. mw := multipart.NewWriter(buf)
  3. defer mw.Close()
  4. f, err := os.Open("C:\\Users\\vembu\\Desktop\\hi.txt")
  5. if err != nil {
  6. // Handle error
  7. }
  8. defer f.Close()
  9. ff, err := mw.CreateFormFile("name", "hi.txt")
  10. if err != nil {
  11. // Handle error
  12. }
  13. if _, err = io.Copy(ff, f); err != nil {
  14. // Handle error
  15. }
  16. // TODO: INCLUDE OTHER FIELDS/PARAMS IN URL
  17. r, err := http.NewRequest("POST", "https://upload.box.com/api/2.0/files/content", buf)
  18. if err != nil {
  19. // Handle error
  20. }
  21. r.Header.Set("Content-Type", mw.FormDataContentType())
  22. // TODO: ADD YOUR OTHER HEADER FIELDS
  23. // Do the call: upload file
  24. client := &http.Client{}
  25. resp, err := client.Do(r)
  26. if err != nil {
  27. // Handle error
  28. }
  29. if resp.StatusCode != http.StatusOK {
  30. fmt.Printf("Error: %v", resp.Status)
  31. }

huangapple
  • 本文由 发表于 2015年5月11日 13:58:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/30160508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定