Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amosavian/AMSMB2
Swift framework to connect SMB2/3 shares
https://github.com/amosavian/AMSMB2
ios smb smb2 smb3 swift
Last synced: 8 days ago
JSON representation
Swift framework to connect SMB2/3 shares
- Host: GitHub
- URL: https://github.com/amosavian/AMSMB2
- Owner: amosavian
- License: lgpl-2.1
- Created: 2018-05-19T21:40:16.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-19T18:24:00.000Z (about 2 months ago)
- Last Synced: 2024-10-20T06:48:56.542Z (about 2 months ago)
- Topics: ios, smb, smb2, smb3, swift
- Language: Swift
- Homepage:
- Size: 37.4 MB
- Stars: 253
- Watchers: 16
- Forks: 78
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - AMSMB2 - Swift framework to connect SMB 2/3 shares for iOS. (Files / Getting Started)
- awesome-ios-star - AMSMB2 - Swift framework to connect SMB 2/3 shares for iOS. (Files / Getting Started)
README
# AMSMB2
This is small Swift library for iOS, macOS and tvOS which wraps [libsmb2](https://github.com/sahlberg/libsmb2) and allows to connect a SMB2/3 share and do file operation.
[![Swift Version Compatibility][swift-version-image]][swift-version-url]
[![Platform Compatibility ][platform-image]][platform-url]
[![License][license-image]][license-url]
[![Release version][release-image]][release-url]## Getting Started
To use AMSMB2, add the following dependency to your Package.swift:
```swift
dependencies: [
.package(url: "https://github.com/amosavian/AMSMB2", .upToNextMinor(from: "3.0.0"))
]
```You can then add the specific product dependency to your target:
```swift
dependencies: [
.product(name: "AMSMB2", package: "AMSMB2"),
]
```## Usage
Just read inline help to find what each function does. It's straightforward. It's thread safe.
To do listing files in directory and file operations you must use this template:
```swift
import AMSMB2class SMBClient: @unchecked Sendable {
/// connect to: `smb://[email protected]/share`
let serverURL = URL(string: "smb://XXX.XXX.XX.XX")!
let credential = URLCredential(user: "guest", password: "", persistence: URLCredential.Persistence.forSession)
let share = "share"
lazy private var client = SMB2Manager(url: self.serverURL, credential: self.credential)!
private func connect() async throws -> SMB2Manager {
// AMSMB2 can handle queueing connection requests
try await client.connectShare(name: self.share)
return self.client
}
func listDirectory(path: String) {
Task {
do {
let client = try await connect()
let files = try await client.contentsOfDirectory(atPath: path)
for entry in files {
print(
"name:", entry[.nameKey] as! String,
", path:", entry[.pathKey] as! String,
", type:", entry[.fileResourceTypeKey] as! URLFileResourceType,
", size:", entry[.fileSizeKey] as! Int64,
", modified:", entry[.contentModificationDateKey] as! Date,
", created:", entry[.creationDateKey] as! Date)
}
} catch {
print(error)
}
}
}
func moveItem(path: String, to toPath: String) {
Task {
do {
let client = try await self.connect()
try await client.moveItem(atPath: path, toPath: toPath)
print("\(path) moved successfully.")
// Disconnecting is optional, it will be called eventually
// when `AMSMB2` object is freed.
// You may call it explicitly to detect errors.
try await client.disconnectShare()
} catch {
print(error)
}
}
}
}```
## License
While source code shipped with project is MIT licensed, but it has static link to `libsmb2` which is `LGPL v2.1`, consequently the whole project becomes `LGPL v2.1`.
You **must** link this library dynamically to your app if you intend to distribute your app on App Store.
[swift-version-image]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Famosavian%2FAMSMB2%2Fbadge%3Ftype%3Dswift-versions
[swift-version-url]: https://swiftpackageindex.com/amosavian/AMSMB2
[platform-image]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Famosavian%2FAMSMB2%2Fbadge%3Ftype%3Dplatforms
[platform-url]: https://swiftpackageindex.com/amosavian/AMSMB2
[license-image]: https://img.shields.io/github/license/amosavian/AMSMB2.svg
[license-url]: LICENSE
[release-image]: https://img.shields.io/github/release/amosavian/AMSMB2.svg
[release-url]: https://github.com/amosavian/AMSMB2/releases