title: Golang file upload to s3 using OS Open
body: <p>I am trying to upload an Image to my s3 account using Golang and the amazon s3 api . I can get the imagine uploaded if I hard code the direct path such as </p>
  
  <pre><code>file, err :=  os.Open("/Users/JohnSmith/Documents/pictures/cars.jpg")
      defer file.Close()
      if err != nil {
          fmt.Printf("err opening file: %s", err)
      }
  </code></pre>
  
  <p>if I hard code the file path like that then the picture will be uploaded to my s3 account . However that approach is not good as I can't obviously hard code the direct image path to every image that I want to upload . My question is how can I upload images without having to Hardcode the path . This will be apart of an API where users will upload images so I clearly can not have a hard coded path . This is my code first the HTML</p>
  
  <pre><code>&lt;form method="post" enctype="multipart/form-data"  action="profile_image"&gt;
      &lt;h2&gt;Image Upload&lt;/h2&gt;
      &lt;p&gt;&lt;input type="file" name="file" id="file"/&gt; &lt;/p&gt;
         &lt;p&gt; &lt;input type="submit" value="Upload Image"&gt;&lt;/p&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>then this is my HTTP Post function method</p>
  
  <pre><code>func UploadProfile(w http.ResponseWriter, r *http.Request) {
  
      r.ParseForm()
        var resultt string
      resultt = "Hi"
  
      sess, _ := session.NewSession(&amp;aws.Config{
          Region:      aws.String("us-west-2"),
          Credentials: credentials.NewStaticCredentials(aws_access_key_id,aws_secret_access_key, ""),
  
  
      })
      svc := s3.New(sess)
  
  file, err :=  os.Open("Users/JohnSmith/Documents/pictures/cars.jpg")
  defer file.Close()
  if err != nil {
      fmt.Printf("err opening file: %s", err)
  }
  
  
  
  fileInfo, _ := file.Stat()
  size := fileInfo.Size()
  buffer := make([]byte, size) // read file content to buffer
  
  file.Read(buffer)
  fileBytes := bytes.NewReader(buffer)
  fileType := http.DetectContentType(buffer)
  path := file.Name()
  params := &amp;s3.PutObjectInput{
      Bucket: aws.String("my-bucket"),
      Key: aws.String(path),
      Body: fileBytes,
      ContentLength: aws.Int64(size),
      ContentType: aws.String(fileType),
  }
  resp, err := svc.PutObject(params)
  if err != nil {
      fmt.Printf("bad response: %s", err)
  }
  fmt.Printf("response %s", awsutil.StringValue(resp))
  
  }
  </code></pre>
  
  <p>That is my full code above however when I try to do something such as</p>
  
  <pre><code>file, err :=  os.Open("file")
      defer file.Close()
      if err != nil {
          fmt.Printf("err opening file: %s", err)
      }
  </code></pre>
  
  <p>I get the following error</p>
  
  <pre><code>http: panic serving [::1]:55454: runtime error: invalid memory address or nil pointer dereference
  goroutine 7 [running]:
  err opening file: open file: no such file or directorynet/http.(*conn).serve.func1(0xc420076e80)
  </code></pre>
  
  <p>I can't use absolute path <em>(filepath.Abs())</em> because some of the files will be outside of the GoPath and as stated other users will be uploading. Is there anyway that I can get a relative path ..</p>