# S4 quickstart

> The complete flow from signing up and verifying your identity, creating a bucket, uploading and accessing files, to creating a sub-user, granting permissions, and connecting to Bitiful S4 with an SDK.

Follow this page and you'll end up with a working bucket, a file you can access, and a set of AccessKeys you can use in code and third-party tools.

| Step                                 | Where                                            | What you get                                     |
| :----------------------------------- | ------------------------------------------------ | ------------------------------------------------ |
| 1. Sign up and verify your identity  | Console                                          | An account that can create buckets and sub-users |
| 2. Create a bucket                   | **Object Storage (对象存储) › Bucket list (桶列表)**    | A globally unique bucket name                    |
| 3. Upload a file                     | The bucket's **File management (文件管理)**          | An object                                        |
| 4. Access the file                   | The bucket's **File management**                 | A signed link or a public link                   |
| 5. Create a sub-user and keys        | **Object Storage › Sub-users & Keys (子账户\&Key)** | An AccessKey and SecretKey                       |
| 6. Grant permissions to the sub-user | Same as above                                    | Permission to read and write the bucket          |
| 7. Connect with an SDK               | Your code                                        | A working program                                |

## Sign up, create a bucket, and upload

### Sign up and verify your identity

1. Open the [Bitiful console](https://console.bitiful.com/), then sign up and sign in.
2. Open [Account information](https://console.bitiful.com/users), click **Identity verification (实名认证)** on the **Verification status (实名状态)** row, and enter your name and ID number.

You can't create buckets or sub-users until you complete identity verification. If your account balance is below 0, you also see the message "您当前已欠费，请先充值" (you are overdue, please top up first).

### Create a bucket

1. Click **Object Storage › Bucket list (对象存储 › 桶列表)** on the left to open the [bucket list](https://console.bitiful.com/buckets).
2. Click **Create bucket (创建桶)** in the upper-right corner.
3. Enter the **Bucket name (桶名称)**: 4 \~ 63 characters, lowercase letters, digits, and hyphens only, beginning and ending with a letter or digit, and unique across the whole platform.
4. Leave **Public bucket (公开桶)** off for now. You can [make the whole bucket or specific directories public](https://docs.bitiful.com/en/bitiful-s4/handbook/public-read.md) at any time.
5. Click **OK (确定)**.

For more, see [Bucket management](https://docs.bitiful.com/en/bitiful-s4/handbook/buckets.md).

### Upload objects

<details>
<summary>Upload files</summary>



</details>

<details>
<summary>Upload a folder</summary>



</details>

1. In the bucket list, click the bucket name to open **File management (文件管理)**.
2. Click **Upload files (上传文件)** in the upper-right corner.
3. Add files in any of these ways:
   - Drag files or folders into the upload area.
   - Click **Scan files (扫描文件)** and select one or more files.
   - Click **Scan folder (扫描文件夹)** and select a folder. The files inside keep their original directory structure.
4. Click **Upload files** below the table. The **Task list (任务列表)** on the right shows progress.

For more, see [Upload files](https://docs.bitiful.com/en/bitiful-s4/handbook/upload.md).

### Access the file

In **File management**, click **…** on the right of the file:

| Menu                          | What you get                                                      | Best for                                                                                        |
| :---------------------------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| **Copy signed URL (复制签名URL)** | A signed link, valid for 1 hour                                   | Private buckets, temporary sharing                                                              |
| **Copy public URL (复制公开URL)** | `https://bucket-name.s3.bitiful.net/file-path`, with no signature | Files that are already [public](https://docs.bitiful.com/en/bitiful-s4/handbook/public-read.md) |

If the file isn't public, opening the public URL returns `403 AccessDenied`.

> **Warning**
>
> Opening a file in a browser through the bucket default domain (`bucket-name.s3.bitiful.net`) triggers a download instead of a preview. To display images, videos, or pages directly in a web page, access them through a [CDN domain](https://docs.bitiful.com/en/cdn/intro.md).
>
> The bucket default domain also can't download `.apk` or `.ipa` files. These files must be distributed through a CDN.

## Create a sub-user and keys

An AccessKey belongs to a sub-user. Your code and third-party tools such as Rclone and image hosting apps connect to S4 with a sub-user's AccessKey, not your login password.

### Add a sub-user

1. Go to **Object Storage › [Sub-users & Keys (子账户\&Key)](https://console.bitiful.com/accessKey)**.
2. Click **Add sub-user (添加子用户)**, enter a **Sub-user name (子用户名)** (3 \~ 12 English letters or digits, which can't be changed after you add it), and click OK.
3. A dialog shows an **Access Key** and a **Secret Key**. Copy and save them right away.

### Save the keys

The Secret Key is shown only this once. If you didn't save it, your only option is to **Add key (添加 Key)** again. See [Sub-users and AccessKeys](https://docs.bitiful.com/en/bitiful-s4/handbook/access-keys.md).

## Grant permissions to the sub-user

A newly created sub-user has **no permissions**. This is the most common cause of 403 errors when you first connect.

### Open permission settings

In the dialog shown after you add the sub-user, click **Set permissions (设定权限)**. You can also click **Set permissions** for that sub-user later in the **Sub-user management (子用户管理)** table.

### Select permissions

Find your bucket and select **Read (读)**, **Write (including delete) (写（含删除）)**, and **List (列表)**. To open up only a certain directory, click **+ Add directory (+ 添加目录)**.

### Save

Click **Save changes (保存更改)** at the bottom. The change takes effect in about 1 second.

For the operations each permission covers, see [Sub-user permissions](https://docs.bitiful.com/en/bitiful-s4/handbook/permissions.md).

## Connect with an SDK

S4 is S3 compatible. With any S3 SDK, you only need to change four settings:

| Setting           | Value                     |
| :---------------- | ------------------------- |
| Endpoint          | `https://s3.bitiful.net`  |
| Region            | `cn-east-1`               |
| Access Key ID     | The sub-user's Access Key |
| Secret Access Key | The sub-user's Secret Key |

The examples below upload a text file and then generate a download link that is valid for 10 minutes. Before you run them, set the environment variables `S4_ACCESS_KEY` and `S4_SECRET_KEY`, and replace `my-bucket` with your bucket name.

```javascript title="index.mjs"
// npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
import { S3Client, PutObjectCommand, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const s3 = new S3Client({
  endpoint: "https://s3.bitiful.net",
  region: "cn-east-1",
  credentials: {
    accessKeyId: process.env.S4_ACCESS_KEY,
    secretAccessKey: process.env.S4_SECRET_KEY,
  },
});

const Bucket = "my-bucket";
const Key = "hello.txt";

await s3.send(new PutObjectCommand({ Bucket, Key, Body: "Hello S4", ContentType: "text/plain" }));
const url = await getSignedUrl(s3, new GetObjectCommand({ Bucket, Key }), { expiresIn: 600 });
console.log(url);
```

```python title="main.py"
# pip install boto3
import os

import boto3

s3 = boto3.client(
    "s3",
    endpoint_url="https://s3.bitiful.net",
    region_name="cn-east-1",
    aws_access_key_id=os.environ["S4_ACCESS_KEY"],
    aws_secret_access_key=os.environ["S4_SECRET_KEY"],
)

bucket, key = "my-bucket", "hello.txt"

s3.put_object(Bucket=bucket, Key=key, Body=b"Hello S4", ContentType="text/plain")
url = s3.generate_presigned_url(
    "get_object", Params={"Bucket": bucket, "Key": key}, ExpiresIn=600
)
print(url)
```

```go title="main.go"
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/credentials"
	"github.com/aws/aws-sdk-go-v2/service/s3"
)

func main() {
	ctx := context.Background()
	client := s3.New(s3.Options{
		BaseEndpoint: aws.String("https://s3.bitiful.net"),
		Region:       "cn-east-1",
		Credentials: credentials.NewStaticCredentialsProvider(
			os.Getenv("S4_ACCESS_KEY"), os.Getenv("S4_SECRET_KEY"), ""),
	})

	bucket, key := aws.String("my-bucket"), aws.String("hello.txt")
	if _, err := client.PutObject(ctx, &s3.PutObjectInput{
		Bucket:      bucket,
		Key:         key,
		Body:        strings.NewReader("Hello S4"),
		ContentType: aws.String("text/plain"),
	}); err != nil {
		log.Fatal(err)
	}

	req, err := s3.NewPresignClient(client).PresignGetObject(ctx,
		&s3.GetObjectInput{Bucket: bucket, Key: key},
		s3.WithPresignExpires(10*time.Minute))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(req.URL)
}
```

For other languages, see the [SDK overview](https://docs.bitiful.com/en/developer/sdk/intro.md).

> **Warning**
>
> Never put your SecretKey in a web page, an app, or a public code repository. For uploads from browsers and apps, have your server [issue presigned URLs](https://docs.bitiful.com/en/best-practice/web-uploads.md).

## FAQ

<details>
<summary>The SDK returns 403 AccessDenied</summary>

Check the following in order:

1. Whether the sub-user has been granted access to this bucket with the right permissions selected.
2. Whether the object is inside an authorized directory.
3. Whether the bucket name and endpoint are correct.

Sub-users can't create or delete buckets. Create buckets in the console.

</details>

<details>
<summary>Creating a bucket prompts for identity verification</summary>

Click **Verify now (去认证)** in the prompt, or open [Account information](https://console.bitiful.com/users) directly to complete identity verification.

</details>

<details>
<summary>The file downloads instead of opening</summary>

This is fixed behavior of the bucket default domain. Access the file through a [CDN domain](https://docs.bitiful.com/en/cdn/intro.md) to display it directly in the browser.

</details>

## Next steps

[**Public read and private**](https://docs.bitiful.com/en/bitiful-s4/handbook/public-read.md)

Make the whole bucket public, or only a few of its directories.

[**Connect a CDN**](https://docs.bitiful.com/en/cdn/intro.md)

Distribute files on your own domain and speed up downloads.

[**Image and video processing**](https://docs.bitiful.com/en/coreix/basic.md)

Add parameters to the URL to resize, watermark, and convert formats in real time.

[**Migrate from another cloud**](https://docs.bitiful.com/en/bitiful-s4/handbook/mirror.md)

Use mirror back-to-origin to migrate existing files without interrupting your business.
