# Original file protection

> Block anonymous access to original images and videos, allow access only through styles, and optionally set a default style.

After you turn on original file protection, visitors can't anonymously get original files of the specified types directly, and can't compose their own processing parameters. They can access only the versions you defined in [Styles](https://docs.bitiful.com/en/coreix/handbook/styles.md). It suits photography, e-commerce, asset libraries, and other scenarios where you need to protect high-resolution originals, and it also stops visitors from generating arbitrary sizes.

To get there, go to **Object Storage (对象存储) › [Bucket list (桶列表)](https://console.bitiful.com/buckets)** › click the bucket name › **Media processing (媒体处理) › Original file protection (原文件保护)**.

## Steps to turn it on

### Create a style first

Create at least one style, for example `preview`: `w=1600&fmt=auto`.

### Edit

On the **Original file protection** card, click **Edit (编辑)** and set **Protection status (保护状态)** to **On (do not allow access to original files) (开启（不允许访问原文件）)**.

### Choose types and a default style

Set **Protected media types (保护媒体类型)** and **Default Style (默认Style)**, as described below.

### Save

Click **Save (保存)**, and it takes effect immediately. Original files already cached by a CDN or browser become protected only after you [purge the cache](https://docs.bitiful.com/en/cdn/handbook/cache.md).

- `Protection status` (single choice, default: Off (original files accessible)): Whether protection is on. When off, all types return to normal access.

- `Protected media types` (multiple choice, custom input allowed): Judged by the file's Content-Type. `image/*` and `video/*` are common. You can also choose specific types such as `image/jpeg`, or enter a type that isn't in the list. Choose at least one; otherwise it's the same as not turning it on. All types joined together can't exceed 500 characters.
  
  After you choose `image/*`, individual `image/...` entries are merged. Choosing `*` means all files in the bucket.

- `Default Style` (single choice, default: None (block access)): How to handle access to a protected original file. **None (block access) (无 (阻止访问))**: Returns `403 OriginalFileProtected`. Choose a style: The result processed by that style is returned automatically.

## Access behavior

Take protecting `image/*` with a style `preview` in the bucket as an example:

| Access method                                | Default Style is "None" | Default Style is `preview`                   |
| :------------------------------------------- | ----------------------- | -------------------------------------------- |
| `a.jpg`                                      | 403                     | Returns the `preview` result                 |
| `a.jpg?w=3000`                               | 403                     | Returns the `preview` result; `w` is ignored |
| `a.jpg!style:preview`                        | Normal                  | Normal                                       |
| Presigned URLs and AccessKey-signed requests | Returns the original    | Returns the original                         |
| `data.json`                                  | Normal                  | Normal                                       |

- Both anonymous access and access through CDN domains are protected.
- When the default style is triggered, the response carries the header `x-bitiful-defaultstyle: Enable` to help with troubleshooting.
- [Variable parameters](https://docs.bitiful.com/en/coreix/handbook/styles.md) declared with `*` in the default style can still be overridden by visitors.

## Recommendations

- For an image site, choose `image/*` plus a default style. Old links need no changes and automatically become the downsized or watermarked version.
- For buckets that include web pages, scripts, archives, and similar files, don't choose `*`. `*` plus a default style sends those files into image processing and returns 415.
- After you set a default style, a link's extension may differ from the actual format (for example, `.jpg` returns WebP). This is normal.
- Files with no Content-Type or an incorrect type don't match, so make sure the [type is correct when you upload](https://docs.bitiful.com/en/bitiful-s4/handbook/upload.md).
- To hand an original to a specific user, use a [presigned URL](https://docs.bitiful.com/en/developer/s3/presigned-url.md).

## How it interacts with other features

| Feature                                                                         | Relationship                                                                                                                                                       |
| :------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [Public read](https://docs.bitiful.com/en/bitiful-s4/handbook/public-read.md)   | Whether the file can be read is decided first, then whether it's protected. Original file protection doesn't make private files accessible                         |
| [Access control](https://docs.bitiful.com/en/bitiful-s4/handbook/anti-leech.md) | Both apply at the same time                                                                                                                                        |
| [Style](https://docs.bitiful.com/en/coreix/handbook/styles.md)                  | A style set as the Default Style can't be deleted                                                                                                                  |
| Mirror back-to-origin                                                           | Protection is judged by the type of the file in the bucket. Migrate files that need protection into the bucket in advance and don't rely on the first origin fetch |

## FAQ

<details>
<summary>It returns 403 OriginalFileProtected</summary>

You accessed a protected original file. Use `!style:style-name` instead, or set a Default Style.

</details>

<details>
<summary>The original image is still visible on the CDN after I turned it on</summary>

The CDN has it cached. Purge the corresponding address or directory on the CDN.

</details>

<details>
<summary>I see "请选择存在的style作为默认style"</summary>

The message means "please choose an existing style as the default style". The style was just deleted. Refresh the page and choose again.

</details>

<details>
<summary>I see "超过最大长度限制"</summary>

The message means "exceeds the maximum length". You chose too many types. Use `image/*` and `video/*` instead of choosing them one by one.

</details>
