# Upload Content Attachment

In this article, I would be showing you how to upload an attachment in a Confluence content using the "go-atlassian" library.

## Step 1: Set up the project

1. Create a new directory for your project.
2. Open a terminal and navigate to the project directory.
3. Initialize a new Go module using the following command:

```bash
go mod init <module-name>
```

## Step 2: Install the "go-atlassian" library

In the terminal, run the following command to install the "go-atlassian" library:

```bash
go get -v github.com/ctreminiom/go-atlassian
```

## Step 3: Import the necessary packages

1. Create a new Go file (e.g., `main.go`) in your project directory.
2. Open the file and import the required packages:

{% code fullWidth="true" %}

```go
package main

import (
	"github.com/ctreminiom/go-atlassian/confluence"
	"log"
	"os"
)
```

{% endcode %}

## Step 4: Authenticate with Confluence

In the `main` function, create a new Confluence client and authenticate using your Atlassian URL, username, and API token:

{% code fullWidth="true" %}

```go
func main() {

	atlassianHost := "https://<_jira_instance_>.atlassian.net"
	mailAddress := "<your_mail>"
	apiToken := "<your_api_token>"

	client, err := confluence.New(nil, atlassianHost )
	if err != nil {
		log.Fatal(err)
	}

	client.Auth.SetBasicAuth(mailAddress, apiToken)
}
```

{% endcode %}

## Step 5: Upload an attachment to a Confluence page

* Define the necessary variables for the page ID, file path, and file name:

{% code fullWidth="true" %}

```go
pageID := "76513281"
filePath := "confluence/mocks/mock.png"
fileName := "mock-00.png"
```

{% endcode %}

* Open the file using the provided file path:

{% code fullWidth="true" %}

```go
absolutePath, err := filepath.Abs(filePath)
if err != nil {
	log.Fatal(err)
}

log.Println("Using the path", absolutePath)

reader, err := os.Open(absolutePath)
if err != nil {
	log.Fatal(err)
}

defer reader.Close()
```

{% endcode %}

* Upload the attachment using the `Content.Attachment.Create()`method and provide the page ID, file name, and file content:

{% code fullWidth="true" %}

```go
attachmentsPage, response, err := client.Content.Attachment.Create(context.Background(), pageID, "current", fileName, reader)
if err != nil {

	if response != nil {
		if response.Code == http.StatusBadRequest {
			log.Println(response.Code)
		}
	}
	log.Println(response.Endpoint)
	log.Fatal(err)
}

log.Println("Endpoint:", response.Endpoint)
log.Println("Status Code:", response.Code)

for _, attachment := range attachmentsPage.Results {
	log.Println(attachment.ID, attachment.Title)
}
```

{% endcode %}

{% hint style="info" %}
Make sure to replace `<CONFLUENCE_PAGE_ID>`, `<FILE_PATH>`, and `<FILE_NAME>` with the actual values for your use case.
{% endhint %}

## Step 6: Run the program

1. Save the `main.go` file.
2. In the terminal, navigate to your project directory.
3. Execute the following command to run the program:

```bash
go run main.go
```

This will upload the specified file as an attachment to the Confluence page with the provided page ID.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.go-atlassian.io/cookbooks/upload-content-attachment.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
