> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 将 blob 上传到 Title Storage

> 使用 XblTitleStorageUploadBlobAsync 将 blob 上传到 XBOX 服务 Title Storage 的扁平 C 代码示例,并提供块大小及缓冲区生存期指南。

本主题演示如何使用以下扁平 C 代码示例将 blob 上传到 XBOX 服务 Title Storage。

## 关于 XblTitleStorageUploadBlobAsync 参数的说明

* 包含要上传的 blob 数据的缓冲区在异步操作期间必须始终可用。在上传进行期间不应修改该缓冲区。

* 如果未提供 `preferredUploadBlockSize`,或所提供的值不在可接受范围内,此方法将使用默认大小。最小值为 1,024 字节。最大值为 4,194,304 字节。默认值为 262,144 字节。

* 如果二进制 blob 超过 `preferredUploadBlockSize`,则将其分为多个块上传。在这种情况下,我们建议使用较大的块大小。如果出现超时,应使用较小的块大小重试上传。

#### Flat C

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = blobBuffer.get();
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock };    // Take over ownership of XAsyncBlock*.
    std::unique_ptr<std::vector<uint8_t>> blobBuffer{ static_cast<std::vector<uint8_t>*>(asyncBlock->context) };
    HRESULT hr = XblTitleStorageUploadBlobResult(asyncBlock, &blobMetadata);
};

XblTitleStorageBlobMetadata blobMetadata{};
pal::strcpy(blobMetadata.displayName, displayName.size() + 1, displayName.c_str());
pal::strcpy(blobMetadata.serviceConfigurationId, XBL_SCID_LENGTH, scid);
pal::strcpy(blobMetadata.blobPath, blobPath.size() + 1, blobPath.c_str());
blobMetadata.storageType = storageType;
blobMetadata.blobType = blobType;
time(&blobMetadata.clientTimestamp);

HRESULT hr = XblTitleStorageUploadBlobAsync(
    xboxLiveContext,
    blobMetadata,
    reinterpret_cast<const uint8_t*>(blobBuffer->data()),
    blobBufferSize,
    eTagMatchCondition,
    preferredUploadBlockSize, //    Optional .
    asyncBlock.get()
);

if (SUCCEEDED(hr))
{
    // The call succeeded, so release std::unique_ptr ownership of XAsyncBlock* because
    // the callback takes over ownership. If the call fails, however, std::unique_ptr keeps
    // ownership and deletes XAsyncBlock*.
    asyncBlock.release();
    blobBuffer.release();
}
```

## 另请参阅

[XAsyncBlock](/reference/system/xasync/structs/xasyncblock)

[XblTitleStorageBlobMetadata](/reference/live/xsapi-c/title_storage_c/structs/xbltitlestorageblobmetadata)

[XblTitleStorageUploadBlobAsync](/reference/live/xsapi-c/title_storage_c/functions/xbltitlestorageuploadblobasync)

[XblTitleStorageUploadBlobResult](/reference/live/xsapi-c/title_storage_c/functions/xbltitlestorageuploadblobresult)


## Related topics

- [Title Storage 示例代码](/zh-CN/services/xbox-services/storage/title-storage/how-to/live-title-storage-howto-nav.md)
- [XblTitleStorageUploadBlobAsync](/zh-CN/reference/live/xsapi-c/title_storage_c/functions/xbltitlestorageuploadblobasync.md)
- [Global Storage 工具 (GlobalStorage.exe)](/zh-CN/tools/tools-services/live-global-storage-tool.md)
- [操作指南](/zh-CN/services/xbox-services/storage/title-storage/how-to/index.md)
- [发布你的第一个用户生成内容](/zh-CN/services/playfab/economy-monetization/economy-v2/tutorials/publish-ugc.md)
